File: testRandom.cc

package info (click to toggle)
clhep 2.1.4.1%2Bdfsg-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 10,012 kB
  • sloc: cpp: 50,094; sh: 6,694; makefile: 2,694; perl: 28
file content (354 lines) | stat: -rwxr-xr-x 14,269 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// -*- C++ -*-
// $Id: testRandom.cc,v 1.2 2010/06/16 17:24:53 garren Exp $
// ----------------------------------------------------------------------
#include "CLHEP/Units/GlobalPhysicalConstants.h"  // used to provoke shadowing warnings
#include "CLHEP/Random/Randomize.h"
#include <iostream>
#include <cstdlib>	// for exit()

using std::cout;
using std::endl;
using std::cin;
using namespace CLHEP;

HepJamesRandom theJamesEngine;
RandEngine theRandEngine;
DRand48Engine theDRand48Engine;
RanluxEngine theRanluxEngine(19780503,4);
Ranlux64Engine theRanlux64Engine;
RanecuEngine theRanecuEngine;
Hurd160Engine theHurd160Engine;
Hurd288Engine theHurd288Engine;
MTwistEngine theMTwistEngine;
RanshiEngine theRanshiEngine;
DualRand theDualRandEngine;
TripleRand theTripleRandEngine;

void init()
{
   HepRandom r;
   cout << "r=" << r() << endl;

   cout << endl << endl;
   cout << "---------------------------- Random shooting test -----------------------------" << endl;
   cout << "                             --------------------                              " << endl;
   cout << " >>> Random Engines available <<<" << endl << endl;
   cout << "   > HepJamesRandom (default)" << endl;
   cout << "   > Rand" << endl;
   cout << "   > DRand48" << endl;
   cout << "   > Ranlux" << endl;
   cout << "   > Ranlux64" << endl;
   cout << "   > Ranecu" << endl;
   cout << "   > Hurd160" << endl;
   cout << "   > Hurd288" << endl;
   cout << "   > MTwist" << endl;
   cout << "   > Ranshi" << endl;
   cout << "   > DualRand" << endl;
   cout << "   > TripleRand" << endl << endl;
   cout << "                   -----  Press <ENTER> to continue  -----";
   if ( cin.get() != '\n') exit(0);
   cout << endl;

}  // end init()

void layout()
{
   float m1=3.0;
   const int size=5;
   double vect[size];

   cout << " Flat ]0,1[          : " << RandFlat::shoot() << endl;
   cout << " Flat ]0,5[          : " << RandFlat::shoot(5) << endl;
   cout << " Flat ]-5,3[         : " << RandFlat::shoot(-5,3) << endl;
   cout << " Exp (m=1)           : " << RandExponential::shoot() << endl;
   cout << " Exp (m=3)           : " << RandExponential::shoot(3) << endl;
   cout << " Gauss (m=1)         : " << RandGauss::shoot() << endl;
   cout << " Gauss (m=3,v=1)     : " << RandGauss::shoot(3,1) << endl;
   cout << " Wigner(1,0.2)       : " << RandBreitWigner::shoot(1,0.2) << endl;
   cout << " Wigner(1,0.2,1)     : " << RandBreitWigner::shoot(1,0.2,1) << endl;
   cout << " Wigner2(1,0.2)      : " << RandBreitWigner::shootM2(1,0.2) << endl;
   cout << " Wigner2(1,0.2,1)    : " << RandBreitWigner::shootM2(1,0.2,1) << endl;
   cout << " IntFlat [0,99[      : " << RandFlat::shootInt(99) << endl;
   cout << " IntFlat [-99,37[    : " << RandFlat::shootInt(-99,37) << endl;
   cout << " Poisson (m=3.0)     : " << RandPoisson::shoot(m1) << endl;
   cout << " Binomial(n=1,p=0.5) : " << RandBinomial::shoot() << endl;
   cout << " Binomial(n=-5,p=0.3): " << RandBinomial::shoot(-5,0.3) << endl;
   cout << " ChiSqr (a=1)        : " << RandChiSquare::shoot() << endl;
   cout << " ChiSqr (a=-5)       : " << RandChiSquare::shoot(-5) << endl;
   cout << " Gamma (k=1,l=1)     : " << RandGamma::shoot() << endl;
   cout << " Gamma (k=3,l=0.5)   : " << RandGamma::shoot(3,0.5) << endl;
   cout << " StudT (a=1)         : " << RandStudentT::shoot() << endl;
   cout << " StudT (a=2.5)       : " << RandStudentT::shoot(2.5) << endl;
   cout << endl;
   cout << " Shooting an array of 5 flat numbers ..." << endl << endl;
   RandFlat::shootArray(size,vect);
   for ( int i=0; i<size; ++i )
     cout << " " << vect[i];
   cout << endl << endl;
}   // end layout() 

void dist_layout()
{
   float m1=3.0;
   const int size=5;
   double vect[size];

   HepJamesRandom aJamesEngine;
   RandEngine aRandEngine;
   DRand48Engine aDRand48Engine;
   RanluxEngine aRanluxEngine(19780503,4);
   Ranlux64Engine aRanlux64Engine;
   RanecuEngine aRanecuEngine;
   Hurd288Engine aHurd288Engine;
   MTwistEngine aMTwistEngine;
   RanshiEngine aRanshiEngine;
   TripleRand aTripleRandEngine;

   RandFlat aFlatObj(aJamesEngine);
   RandExponential anExponentialObj(aRandEngine);
   RandGauss aGaussObj(aDRand48Engine);
   RandBreitWigner aBreitObj(aRanluxEngine);
   RandPoisson aPoissonObj(aRanecuEngine);
   RandBinomial aBinomialObj(aHurd288Engine);
   RandChiSquare aChiSquareObj(aMTwistEngine);
   RandGamma aGammaObj(aRanshiEngine);
   RandStudentT aStudentTObj(aTripleRandEngine);

   cout << "                   -----  Press <ENTER> to continue  -----";
   if ( cin.get() != '\n') exit(0);
   cout << endl << endl;
   cout << "-------------------- Shooting test on distribution objects --------------------" << endl;
   cout << endl;
   cout << " Flat ]0,1[          : " << aFlatObj.fire() << endl;
   cout << " Flat ]0,5[          : " << aFlatObj.fire(5) << endl;
   cout << " Flat ]-5,3[         : " << aFlatObj.fire(-5,3) << endl;
   cout << " Exp (m=1)           : " << anExponentialObj.fire() << endl;
   cout << " Exp (m=3)           : " << anExponentialObj.fire(3) << endl;
   cout << " Gauss (m=1)         : " << aGaussObj.fire() << endl;
   cout << " Gauss (m=3,v=1)     : " << aGaussObj.fire(3,1) << endl;
   cout << " Wigner(1,0.2)       : " << aBreitObj.fire(1,0.2) << endl;
   cout << " Wigner(1,0.2,1)     : " << aBreitObj.fire(1,0.2,1) << endl;
   cout << " Wigner2(1,0.2)      : " << aBreitObj.fireM2(1,0.2) << endl;
   cout << " Wigner2(1,0.2,1)    : " << aBreitObj.fireM2(1,0.2,1) << endl;
   cout << " IntFlat [0,99[      : " << aFlatObj.fireInt(99) << endl;
   cout << " IntFlat [-99,37[    : " << aFlatObj.fireInt(-99,37) << endl;
   cout << " Poisson (m=3.0)     : " << aPoissonObj.fire(m1) << endl;
   cout << " Binomial(n=1,p=0.5) : " << aBinomialObj.fire() << endl;
   cout << " Binomial(n=-5,p=0.3): " << aBinomialObj.fire(-5,0.3) << endl;
   cout << " ChiSqr (a=1)        : " << aChiSquareObj.fire() << endl;
   cout << " ChiSqr (a=-5)       : " << aChiSquareObj.fire(-5) << endl;
   cout << " Gamma (k=1,l=1)     : " << aGammaObj.fire() << endl;
   cout << " Gamma (k=3,l=0.5)   : " << aGammaObj.fire(3,0.5) << endl;
   cout << " StudT (a=1)         : " << aStudentTObj.fire() << endl;
   cout << " StudT (a=2.5)       : " << aStudentTObj.fire(2.5) << endl;
   cout << endl;
   cout << " Shooting an array of 5 flat numbers ..." << endl << endl;
   aFlatObj.fireArray(size,vect);
   for ( int i=0; i<size; ++i )
     cout << " " << vect[i];
   cout << endl << endl;
   cout << "                   -----  Press <ENTER> to continue  -----";
   if ( cin.get() != '\n') exit(0);
}   // end dist_layout() 

void user_layout()
{
   float m1=3.0;
   const int size=5;
   double vect[size];
   char sel;
   HepRandomEngine* anEngine;

   cout << endl << endl;
   cout << "-------------------- Shooting test skeeping the generator ---------------------" << endl;
   cout << endl;
   cout << " >>> Select a Random Engine <<<" << endl << endl;
   cout << "   a. HepJamesRandom (default)" << endl;
   cout << "   b. Rand" << endl;
   cout << "   c. DRand48" << endl;
   cout << "   d. Ranlux" << endl;
   cout << "   e. Ranlux64" << endl;
   cout << "   f. Ranecu" << endl;
   cout << "   g. Hurd160" << endl;
   cout << "   h. Hurd288" << endl;
   cout << "   i. MTwist" << endl;
   cout << "   j. Ranshi" << endl;
   cout << "   k. DualRand" << endl;
   cout << "   l. TripleRand" << endl << endl;
   cout << " > ";
   cin >> sel;
   while ((sel!='a')&&(sel!='b')&&(sel!='c')&&(sel!='d')&&(sel!='e')&&
          (sel!='f')&&(sel!='g')&&(sel!='h')&&(sel!='i')&&(sel!='j')&&
          (sel!='k')&&(sel!='l')) {
     cout << endl << " >>> Choice not legal !!  [a..l]<<<" << endl;
     cin >> sel;
   }

   switch (sel) {
     case 'a':
       anEngine = &theJamesEngine;
       break;
     case 'b':
       anEngine = &theRandEngine;
       break;
     case 'c':
       anEngine = &theDRand48Engine;
       break;
     case 'd':
       anEngine = &theRanluxEngine;
       break;
     case 'e':
       anEngine = &theRanlux64Engine;
       break;
     case 'f':
       anEngine = &theRanecuEngine;
       break;
     case 'g':
       anEngine = &theHurd160Engine;
       break;
     case 'h':
       anEngine = &theHurd288Engine;
       break;
     case 'i':
       anEngine = &theMTwistEngine;
       break;
     case 'j':
       anEngine = &theRanshiEngine;
       break;
     case 'k':
       anEngine = &theDualRandEngine;
       break;
     case 'l':
       anEngine = &theTripleRandEngine;
       break;
     default:
       anEngine = &theJamesEngine;
       break;
   }
   cout << endl;
 
   cout << " Flat ]0,1[          : " << RandFlat::shoot(anEngine) << endl;
   cout << " Flat ]0,5[          : " << RandFlat::shoot(anEngine,5) << endl;
   cout << " Flat ]-5,3[         : " << RandFlat::shoot(anEngine,-5,3) << endl;
   cout << " Exp (m=1)           : " << RandExponential::shoot(anEngine) << endl;
   cout << " Exp (m=3)           : " << RandExponential::shoot(anEngine,3) << endl;
   cout << " Gauss (m=1)         : " << RandGauss::shoot(anEngine) << endl;
   cout << " Gauss (m=3,v=1)     : " << RandGauss::shoot(anEngine,3,1) << endl;
   cout << " Wigner(1,0.2)       : " << RandBreitWigner::shoot(anEngine,1,0.2) << endl;
   cout << " Wigner(1,0.2,1)     : " << RandBreitWigner::shoot(anEngine,1,0.2,1) << endl;
   cout << " Wigner2(1,0.2)      : " << RandBreitWigner::shootM2(anEngine,1,0.2) << endl;
   cout << " Wigner2(1,0.2,1)    : " << RandBreitWigner::shootM2(anEngine,1,0.2,1) << endl;
   cout << " IntFlat [0,99[      : " << RandFlat::shootInt(anEngine,99) << endl;
   cout << " IntFlat [-99,37[    : " << RandFlat::shootInt(anEngine,-99,37) << endl;
   cout << " Poisson (m=3.0)     : " << RandPoisson::shoot(anEngine,m1) << endl;
   cout << " Binomial(n=1,p=0.5) : " << RandBinomial::shoot(anEngine) << endl;
   cout << " Binomial(n=-5,p=0.3): " << RandBinomial::shoot(anEngine,-5,0.3) << endl;
   cout << " ChiSqr (a=1)        : " << RandChiSquare::shoot(anEngine) << endl;
   cout << " ChiSqr (a=-5)       : " << RandChiSquare::shoot(anEngine,-5) << endl;
   cout << " Gamma (k=1,l=1)     : " << RandGamma::shoot(anEngine) << endl;
   cout << " Gamma (k=3,l=0.5)   : " << RandGamma::shoot(anEngine,3,0.5) << endl;
   cout << " StudT (a=1)         : " << RandStudentT::shoot(anEngine) << endl;
   cout << " StudT (a=2.5)       : " << RandStudentT::shoot(anEngine,2.5) << endl;
   cout << endl;
   cout << " Shooting an array of 5 flat numbers ..." << endl << endl;
   RandFlat::shootArray(anEngine,size,vect);
   for ( int i=0; i<size; ++i )
     cout << " " << vect[i];
   cout << endl << endl;
}   // end layout() 

void start_test()
{
   cout << "-------------------------  Test on HepJamesRandom  ----------------------------" << endl;
   cout << endl;
   layout();
   cout << "                   -----  Press <ENTER> to continue  -----";
   if ( cin.get() != '\n') exit(0);
   cout << endl;
   cout << "---------------------------  Test on RandEngine  ------------------------------" << endl;
   cout << endl;
   HepRandom::setTheEngine(&theRandEngine);
   layout();
   cout << "                   -----  Press <ENTER> to continue  -----";
   if ( cin.get() != '\n') exit(0);
   cout << endl;
   cout << "-------------------------  Test on DRand48Engine  -----------------------------" << endl;
   cout << endl;
   HepRandom::setTheEngine(&theDRand48Engine);
   layout();
   cout << "                   -----  Press <ENTER> to continue  -----";
   if ( cin.get() != '\n') exit(0);
   cout << endl;
   cout << "---------------------  Test on RanluxEngine (luxury 4) ------------------------" << endl;
   cout << endl;
   HepRandom::setTheEngine(&theRanluxEngine);
   layout();
   cout << "                   -----  Press <ENTER> to continue  -----";
   if ( cin.get() != '\n') exit(0);
   cout << endl;
   cout << "-------------------------  Test on Ranlux64Engine -----------------------------" << endl;
   cout << endl;
   HepRandom::setTheEngine(&theRanlux64Engine);
   layout();
   cout << "                   -----  Press <ENTER> to continue  -----";
   if ( cin.get() != '\n') exit(0);
   cout << endl;
   cout << "--------------------------  Test on RanecuEngine ------------------------------" << endl;
   cout << endl;
   HepRandom::setTheEngine(&theRanecuEngine);
   layout();
   cout << "                   -----  Press <ENTER> to continue  -----";
   if ( cin.get() != '\n') exit(0);
   cout << endl;
   cout << "-------------------------  Test on Hurd160Engine ------------------------------" << endl;
   cout << endl;
   HepRandom::setTheEngine(&theHurd160Engine);
   layout();
   cout << "                   -----  Press <ENTER> to continue  -----";
   if ( cin.get() != '\n') exit(0);
   cout << endl;
   cout << "-------------------------  Test on Hurd288Engine ------------------------------" << endl;
   cout << endl;
   HepRandom::setTheEngine(&theHurd288Engine);
   layout();
   cout << "                   -----  Press <ENTER> to continue  -----";
   if ( cin.get() != '\n') exit(0);
   cout << endl;
   cout << "--------------------------  Test on MTwistEngine ------------------------------" << endl;
   cout << endl;
   HepRandom::setTheEngine(&theMTwistEngine);
   layout();
   cout << "                   -----  Press <ENTER> to continue  -----";
   if ( cin.get() != '\n') exit(0);
   cout << endl;
   cout << "--------------------------  Test on RanshiEngine ------------------------------" << endl;
   cout << endl;
   HepRandom::setTheEngine(&theRanshiEngine);
   layout();
   cout << "                   -----  Press <ENTER> to continue  -----";
   if ( cin.get() != '\n') exit(0);
   cout << endl;
   cout << "-------------------------  Test on DualRandEngine -----------------------------" << endl;
   cout << endl;
   HepRandom::setTheEngine(&theDualRandEngine);
   layout();
   cout << "                   -----  Press <ENTER> to continue  -----";
   if ( cin.get() != '\n') exit(0);
   cout << endl;
   cout << "------------------------  Test on TripleRandEngine ----------------------------" << endl;
   cout << endl;
   HepRandom::setTheEngine(&theTripleRandEngine);
   layout();
   dist_layout();
   user_layout();
}  // end start_test()


int main() {

   init();
   start_test();
   
   return 0;
}