CLHEP VERSION Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

testRandom.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 // $Id: testRandom.cc,v 1.2 2010/06/16 17:24:53 garren Exp $
3 // ----------------------------------------------------------------------
4 #include "CLHEP/Units/GlobalPhysicalConstants.h" // used to provoke shadowing warnings
5 #include "CLHEP/Random/Randomize.h"
6 #include <iostream>
7 #include <cstdlib> // for exit()
8 
9 using std::cout;
10 using std::endl;
11 using std::cin;
12 using namespace CLHEP;
13 
26 
27 void init()
28 {
29  HepRandom r;
30  cout << "r=" << r() << endl;
31 
32  cout << endl << endl;
33  cout << "---------------------------- Random shooting test -----------------------------" << endl;
34  cout << " -------------------- " << endl;
35  cout << " >>> Random Engines available <<<" << endl << endl;
36  cout << " > HepJamesRandom (default)" << endl;
37  cout << " > Rand" << endl;
38  cout << " > DRand48" << endl;
39  cout << " > Ranlux" << endl;
40  cout << " > Ranlux64" << endl;
41  cout << " > Ranecu" << endl;
42  cout << " > Hurd160" << endl;
43  cout << " > Hurd288" << endl;
44  cout << " > MTwist" << endl;
45  cout << " > Ranshi" << endl;
46  cout << " > DualRand" << endl;
47  cout << " > TripleRand" << endl << endl;
48  cout << " ----- Press <ENTER> to continue -----";
49  if ( cin.get() != '\n') exit(0);
50  cout << endl;
51 
52 } // end init()
53 
54 void layout()
55 {
56  float m1=3.0;
57  const int size=5;
58  double vect[size];
59 
60  cout << " Flat ]0,1[ : " << RandFlat::shoot() << endl;
61  cout << " Flat ]0,5[ : " << RandFlat::shoot(5) << endl;
62  cout << " Flat ]-5,3[ : " << RandFlat::shoot(-5,3) << endl;
63  cout << " Exp (m=1) : " << RandExponential::shoot() << endl;
64  cout << " Exp (m=3) : " << RandExponential::shoot(3) << endl;
65  cout << " Gauss (m=1) : " << RandGauss::shoot() << endl;
66  cout << " Gauss (m=3,v=1) : " << RandGauss::shoot(3,1) << endl;
67  cout << " Wigner(1,0.2) : " << RandBreitWigner::shoot(1,0.2) << endl;
68  cout << " Wigner(1,0.2,1) : " << RandBreitWigner::shoot(1,0.2,1) << endl;
69  cout << " Wigner2(1,0.2) : " << RandBreitWigner::shootM2(1,0.2) << endl;
70  cout << " Wigner2(1,0.2,1) : " << RandBreitWigner::shootM2(1,0.2,1) << endl;
71  cout << " IntFlat [0,99[ : " << RandFlat::shootInt(99) << endl;
72  cout << " IntFlat [-99,37[ : " << RandFlat::shootInt(-99,37) << endl;
73  cout << " Poisson (m=3.0) : " << RandPoisson::shoot(m1) << endl;
74  cout << " Binomial(n=1,p=0.5) : " << RandBinomial::shoot() << endl;
75  cout << " Binomial(n=-5,p=0.3): " << RandBinomial::shoot(-5,0.3) << endl;
76  cout << " ChiSqr (a=1) : " << RandChiSquare::shoot() << endl;
77  cout << " ChiSqr (a=-5) : " << RandChiSquare::shoot(-5) << endl;
78  cout << " Gamma (k=1,l=1) : " << RandGamma::shoot() << endl;
79  cout << " Gamma (k=3,l=0.5) : " << RandGamma::shoot(3,0.5) << endl;
80  cout << " StudT (a=1) : " << RandStudentT::shoot() << endl;
81  cout << " StudT (a=2.5) : " << RandStudentT::shoot(2.5) << endl;
82  cout << endl;
83  cout << " Shooting an array of 5 flat numbers ..." << endl << endl;
84  RandFlat::shootArray(size,vect);
85  for ( int i=0; i<size; ++i )
86  cout << " " << vect[i];
87  cout << endl << endl;
88 } // end layout()
89 
91 {
92  float m1=3.0;
93  const int size=5;
94  double vect[size];
95 
96  HepJamesRandom aJamesEngine;
97  RandEngine aRandEngine;
98  DRand48Engine aDRand48Engine;
99  RanluxEngine aRanluxEngine(19780503,4);
100  Ranlux64Engine aRanlux64Engine;
101  RanecuEngine aRanecuEngine;
102  Hurd288Engine aHurd288Engine;
103  MTwistEngine aMTwistEngine;
104  RanshiEngine aRanshiEngine;
105  TripleRand aTripleRandEngine;
106 
107  RandFlat aFlatObj(aJamesEngine);
108  RandExponential anExponentialObj(aRandEngine);
109  RandGauss aGaussObj(aDRand48Engine);
110  RandBreitWigner aBreitObj(aRanluxEngine);
111  RandPoisson aPoissonObj(aRanecuEngine);
112  RandBinomial aBinomialObj(aHurd288Engine);
113  RandChiSquare aChiSquareObj(aMTwistEngine);
114  RandGamma aGammaObj(aRanshiEngine);
115  RandStudentT aStudentTObj(aTripleRandEngine);
116 
117  cout << " ----- Press <ENTER> to continue -----";
118  if ( cin.get() != '\n') exit(0);
119  cout << endl << endl;
120  cout << "-------------------- Shooting test on distribution objects --------------------" << endl;
121  cout << endl;
122  cout << " Flat ]0,1[ : " << aFlatObj.fire() << endl;
123  cout << " Flat ]0,5[ : " << aFlatObj.fire(5) << endl;
124  cout << " Flat ]-5,3[ : " << aFlatObj.fire(-5,3) << endl;
125  cout << " Exp (m=1) : " << anExponentialObj.fire() << endl;
126  cout << " Exp (m=3) : " << anExponentialObj.fire(3) << endl;
127  cout << " Gauss (m=1) : " << aGaussObj.fire() << endl;
128  cout << " Gauss (m=3,v=1) : " << aGaussObj.fire(3,1) << endl;
129  cout << " Wigner(1,0.2) : " << aBreitObj.fire(1,0.2) << endl;
130  cout << " Wigner(1,0.2,1) : " << aBreitObj.fire(1,0.2,1) << endl;
131  cout << " Wigner2(1,0.2) : " << aBreitObj.fireM2(1,0.2) << endl;
132  cout << " Wigner2(1,0.2,1) : " << aBreitObj.fireM2(1,0.2,1) << endl;
133  cout << " IntFlat [0,99[ : " << aFlatObj.fireInt(99) << endl;
134  cout << " IntFlat [-99,37[ : " << aFlatObj.fireInt(-99,37) << endl;
135  cout << " Poisson (m=3.0) : " << aPoissonObj.fire(m1) << endl;
136  cout << " Binomial(n=1,p=0.5) : " << aBinomialObj.fire() << endl;
137  cout << " Binomial(n=-5,p=0.3): " << aBinomialObj.fire(-5,0.3) << endl;
138  cout << " ChiSqr (a=1) : " << aChiSquareObj.fire() << endl;
139  cout << " ChiSqr (a=-5) : " << aChiSquareObj.fire(-5) << endl;
140  cout << " Gamma (k=1,l=1) : " << aGammaObj.fire() << endl;
141  cout << " Gamma (k=3,l=0.5) : " << aGammaObj.fire(3,0.5) << endl;
142  cout << " StudT (a=1) : " << aStudentTObj.fire() << endl;
143  cout << " StudT (a=2.5) : " << aStudentTObj.fire(2.5) << endl;
144  cout << endl;
145  cout << " Shooting an array of 5 flat numbers ..." << endl << endl;
146  aFlatObj.fireArray(size,vect);
147  for ( int i=0; i<size; ++i )
148  cout << " " << vect[i];
149  cout << endl << endl;
150  cout << " ----- Press <ENTER> to continue -----";
151  if ( cin.get() != '\n') exit(0);
152 } // end dist_layout()
153 
155 {
156  float m1=3.0;
157  const int size=5;
158  double vect[size];
159  char sel;
160  HepRandomEngine* anEngine;
161 
162  cout << endl << endl;
163  cout << "-------------------- Shooting test skeeping the generator ---------------------" << endl;
164  cout << endl;
165  cout << " >>> Select a Random Engine <<<" << endl << endl;
166  cout << " a. HepJamesRandom (default)" << endl;
167  cout << " b. Rand" << endl;
168  cout << " c. DRand48" << endl;
169  cout << " d. Ranlux" << endl;
170  cout << " e. Ranlux64" << endl;
171  cout << " f. Ranecu" << endl;
172  cout << " g. Hurd160" << endl;
173  cout << " h. Hurd288" << endl;
174  cout << " i. MTwist" << endl;
175  cout << " j. Ranshi" << endl;
176  cout << " k. DualRand" << endl;
177  cout << " l. TripleRand" << endl << endl;
178  cout << " > ";
179  cin >> sel;
180  while ((sel!='a')&&(sel!='b')&&(sel!='c')&&(sel!='d')&&(sel!='e')&&
181  (sel!='f')&&(sel!='g')&&(sel!='h')&&(sel!='i')&&(sel!='j')&&
182  (sel!='k')&&(sel!='l')) {
183  cout << endl << " >>> Choice not legal !! [a..l]<<<" << endl;
184  cin >> sel;
185  }
186 
187  switch (sel) {
188  case 'a':
189  anEngine = &theJamesEngine;
190  break;
191  case 'b':
192  anEngine = &theRandEngine;
193  break;
194  case 'c':
195  anEngine = &theDRand48Engine;
196  break;
197  case 'd':
198  anEngine = &theRanluxEngine;
199  break;
200  case 'e':
201  anEngine = &theRanlux64Engine;
202  break;
203  case 'f':
204  anEngine = &theRanecuEngine;
205  break;
206  case 'g':
207  anEngine = &theHurd160Engine;
208  break;
209  case 'h':
210  anEngine = &theHurd288Engine;
211  break;
212  case 'i':
213  anEngine = &theMTwistEngine;
214  break;
215  case 'j':
216  anEngine = &theRanshiEngine;
217  break;
218  case 'k':
219  anEngine = &theDualRandEngine;
220  break;
221  case 'l':
222  anEngine = &theTripleRandEngine;
223  break;
224  default:
225  anEngine = &theJamesEngine;
226  break;
227  }
228  cout << endl;
229 
230  cout << " Flat ]0,1[ : " << RandFlat::shoot(anEngine) << endl;
231  cout << " Flat ]0,5[ : " << RandFlat::shoot(anEngine,5) << endl;
232  cout << " Flat ]-5,3[ : " << RandFlat::shoot(anEngine,-5,3) << endl;
233  cout << " Exp (m=1) : " << RandExponential::shoot(anEngine) << endl;
234  cout << " Exp (m=3) : " << RandExponential::shoot(anEngine,3) << endl;
235  cout << " Gauss (m=1) : " << RandGauss::shoot(anEngine) << endl;
236  cout << " Gauss (m=3,v=1) : " << RandGauss::shoot(anEngine,3,1) << endl;
237  cout << " Wigner(1,0.2) : " << RandBreitWigner::shoot(anEngine,1,0.2) << endl;
238  cout << " Wigner(1,0.2,1) : " << RandBreitWigner::shoot(anEngine,1,0.2,1) << endl;
239  cout << " Wigner2(1,0.2) : " << RandBreitWigner::shootM2(anEngine,1,0.2) << endl;
240  cout << " Wigner2(1,0.2,1) : " << RandBreitWigner::shootM2(anEngine,1,0.2,1) << endl;
241  cout << " IntFlat [0,99[ : " << RandFlat::shootInt(anEngine,99) << endl;
242  cout << " IntFlat [-99,37[ : " << RandFlat::shootInt(anEngine,-99,37) << endl;
243  cout << " Poisson (m=3.0) : " << RandPoisson::shoot(anEngine,m1) << endl;
244  cout << " Binomial(n=1,p=0.5) : " << RandBinomial::shoot(anEngine) << endl;
245  cout << " Binomial(n=-5,p=0.3): " << RandBinomial::shoot(anEngine,-5,0.3) << endl;
246  cout << " ChiSqr (a=1) : " << RandChiSquare::shoot(anEngine) << endl;
247  cout << " ChiSqr (a=-5) : " << RandChiSquare::shoot(anEngine,-5) << endl;
248  cout << " Gamma (k=1,l=1) : " << RandGamma::shoot(anEngine) << endl;
249  cout << " Gamma (k=3,l=0.5) : " << RandGamma::shoot(anEngine,3,0.5) << endl;
250  cout << " StudT (a=1) : " << RandStudentT::shoot(anEngine) << endl;
251  cout << " StudT (a=2.5) : " << RandStudentT::shoot(anEngine,2.5) << endl;
252  cout << endl;
253  cout << " Shooting an array of 5 flat numbers ..." << endl << endl;
254  RandFlat::shootArray(anEngine,size,vect);
255  for ( int i=0; i<size; ++i )
256  cout << " " << vect[i];
257  cout << endl << endl;
258 } // end layout()
259 
261 {
262  cout << "------------------------- Test on HepJamesRandom ----------------------------" << endl;
263  cout << endl;
264  layout();
265  cout << " ----- Press <ENTER> to continue -----";
266  if ( cin.get() != '\n') exit(0);
267  cout << endl;
268  cout << "--------------------------- Test on RandEngine ------------------------------" << endl;
269  cout << endl;
271  layout();
272  cout << " ----- Press <ENTER> to continue -----";
273  if ( cin.get() != '\n') exit(0);
274  cout << endl;
275  cout << "------------------------- Test on DRand48Engine -----------------------------" << endl;
276  cout << endl;
278  layout();
279  cout << " ----- Press <ENTER> to continue -----";
280  if ( cin.get() != '\n') exit(0);
281  cout << endl;
282  cout << "--------------------- Test on RanluxEngine (luxury 4) ------------------------" << endl;
283  cout << endl;
285  layout();
286  cout << " ----- Press <ENTER> to continue -----";
287  if ( cin.get() != '\n') exit(0);
288  cout << endl;
289  cout << "------------------------- Test on Ranlux64Engine -----------------------------" << endl;
290  cout << endl;
292  layout();
293  cout << " ----- Press <ENTER> to continue -----";
294  if ( cin.get() != '\n') exit(0);
295  cout << endl;
296  cout << "-------------------------- Test on RanecuEngine ------------------------------" << endl;
297  cout << endl;
299  layout();
300  cout << " ----- Press <ENTER> to continue -----";
301  if ( cin.get() != '\n') exit(0);
302  cout << endl;
303  cout << "------------------------- Test on Hurd160Engine ------------------------------" << endl;
304  cout << endl;
306  layout();
307  cout << " ----- Press <ENTER> to continue -----";
308  if ( cin.get() != '\n') exit(0);
309  cout << endl;
310  cout << "------------------------- Test on Hurd288Engine ------------------------------" << endl;
311  cout << endl;
313  layout();
314  cout << " ----- Press <ENTER> to continue -----";
315  if ( cin.get() != '\n') exit(0);
316  cout << endl;
317  cout << "-------------------------- Test on MTwistEngine ------------------------------" << endl;
318  cout << endl;
320  layout();
321  cout << " ----- Press <ENTER> to continue -----";
322  if ( cin.get() != '\n') exit(0);
323  cout << endl;
324  cout << "-------------------------- Test on RanshiEngine ------------------------------" << endl;
325  cout << endl;
327  layout();
328  cout << " ----- Press <ENTER> to continue -----";
329  if ( cin.get() != '\n') exit(0);
330  cout << endl;
331  cout << "------------------------- Test on DualRandEngine -----------------------------" << endl;
332  cout << endl;
334  layout();
335  cout << " ----- Press <ENTER> to continue -----";
336  if ( cin.get() != '\n') exit(0);
337  cout << endl;
338  cout << "------------------------ Test on TripleRandEngine ----------------------------" << endl;
339  cout << endl;
341  layout();
342  dist_layout();
343  user_layout();
344 } // end start_test()
345 
346 
347 int main() {
348 
349  init();
350  start_test();
351 
352  return 0;
353 }
354 
static void setTheEngine(HepRandomEngine *theNewEngine)
Definition: Random.cc:171
static double shoot()
static double shootM2(double a=1.0, double b=0.2)
static double shoot(double a=1.0, double b=0.2)
static double shoot()
static long shootInt(long n)
void fireArray(const int size, double *vect)
Definition: RandFlat.cc:87
long fireInt(long n)
static double shoot()
Definition: RandFlat.cc:60
static void shootArray(const int size, double *vect)
Definition: RandFlat.cc:64
static double shoot()
static double shoot()
Definition: RandGauss.cc:61
static long shoot(double m=1.0)
Definition: RandPoisson.cc:92
static double shoot()
#define exit(x)
void init()
Definition: testRandom.cc:27
RandEngine theRandEngine
Definition: testRandom.cc:15
void user_layout()
Definition: testRandom.cc:154
RanshiEngine theRanshiEngine
Definition: testRandom.cc:23
void start_test()
Definition: testRandom.cc:260
Hurd160Engine theHurd160Engine
Definition: testRandom.cc:20
Ranlux64Engine theRanlux64Engine
Definition: testRandom.cc:18
RanecuEngine theRanecuEngine
Definition: testRandom.cc:19
TripleRand theTripleRandEngine
Definition: testRandom.cc:25
DRand48Engine theDRand48Engine
Definition: testRandom.cc:16
DualRand theDualRandEngine
Definition: testRandom.cc:24
void dist_layout()
Definition: testRandom.cc:90
HepJamesRandom theJamesEngine
Definition: testRandom.cc:14
RanluxEngine theRanluxEngine(19780503, 4)
int main()
Definition: testRandom.cc:347
MTwistEngine theMTwistEngine
Definition: testRandom.cc:22
void layout()
Definition: testRandom.cc:54
Hurd288Engine theHurd288Engine
Definition: testRandom.cc:21