CLHEP VERSION Reference Documentation
CLHEP Home Page CLHEP Documentation CLHEP Bug Reports |
00001 // -*- C++ -*- 00002 // $Id: testRandom.cc,v 1.2 2010/06/16 17:24:53 garren Exp $ 00003 // ---------------------------------------------------------------------- 00004 #include "CLHEP/Units/GlobalPhysicalConstants.h" // used to provoke shadowing warnings 00005 #include "CLHEP/Random/Randomize.h" 00006 #include <iostream> 00007 #include <cstdlib> // for exit() 00008 00009 using std::cout; 00010 using std::endl; 00011 using std::cin; 00012 using namespace CLHEP; 00013 00014 HepJamesRandom theJamesEngine; 00015 RandEngine theRandEngine; 00016 DRand48Engine theDRand48Engine; 00017 RanluxEngine theRanluxEngine(19780503,4); 00018 Ranlux64Engine theRanlux64Engine; 00019 RanecuEngine theRanecuEngine; 00020 Hurd160Engine theHurd160Engine; 00021 Hurd288Engine theHurd288Engine; 00022 MTwistEngine theMTwistEngine; 00023 RanshiEngine theRanshiEngine; 00024 DualRand theDualRandEngine; 00025 TripleRand theTripleRandEngine; 00026 00027 void init() 00028 { 00029 HepRandom r; 00030 cout << "r=" << r() << endl; 00031 00032 cout << endl << endl; 00033 cout << "---------------------------- Random shooting test -----------------------------" << endl; 00034 cout << " -------------------- " << endl; 00035 cout << " >>> Random Engines available <<<" << endl << endl; 00036 cout << " > HepJamesRandom (default)" << endl; 00037 cout << " > Rand" << endl; 00038 cout << " > DRand48" << endl; 00039 cout << " > Ranlux" << endl; 00040 cout << " > Ranlux64" << endl; 00041 cout << " > Ranecu" << endl; 00042 cout << " > Hurd160" << endl; 00043 cout << " > Hurd288" << endl; 00044 cout << " > MTwist" << endl; 00045 cout << " > Ranshi" << endl; 00046 cout << " > DualRand" << endl; 00047 cout << " > TripleRand" << endl << endl; 00048 cout << " ----- Press <ENTER> to continue -----"; 00049 if ( cin.get() != '\n') exit(0); 00050 cout << endl; 00051 00052 } // end init() 00053 00054 void layout() 00055 { 00056 float m1=3.0; 00057 const int size=5; 00058 double vect[size]; 00059 00060 cout << " Flat ]0,1[ : " << RandFlat::shoot() << endl; 00061 cout << " Flat ]0,5[ : " << RandFlat::shoot(5) << endl; 00062 cout << " Flat ]-5,3[ : " << RandFlat::shoot(-5,3) << endl; 00063 cout << " Exp (m=1) : " << RandExponential::shoot() << endl; 00064 cout << " Exp (m=3) : " << RandExponential::shoot(3) << endl; 00065 cout << " Gauss (m=1) : " << RandGauss::shoot() << endl; 00066 cout << " Gauss (m=3,v=1) : " << RandGauss::shoot(3,1) << endl; 00067 cout << " Wigner(1,0.2) : " << RandBreitWigner::shoot(1,0.2) << endl; 00068 cout << " Wigner(1,0.2,1) : " << RandBreitWigner::shoot(1,0.2,1) << endl; 00069 cout << " Wigner2(1,0.2) : " << RandBreitWigner::shootM2(1,0.2) << endl; 00070 cout << " Wigner2(1,0.2,1) : " << RandBreitWigner::shootM2(1,0.2,1) << endl; 00071 cout << " IntFlat [0,99[ : " << RandFlat::shootInt(99) << endl; 00072 cout << " IntFlat [-99,37[ : " << RandFlat::shootInt(-99,37) << endl; 00073 cout << " Poisson (m=3.0) : " << RandPoisson::shoot(m1) << endl; 00074 cout << " Binomial(n=1,p=0.5) : " << RandBinomial::shoot() << endl; 00075 cout << " Binomial(n=-5,p=0.3): " << RandBinomial::shoot(-5,0.3) << endl; 00076 cout << " ChiSqr (a=1) : " << RandChiSquare::shoot() << endl; 00077 cout << " ChiSqr (a=-5) : " << RandChiSquare::shoot(-5) << endl; 00078 cout << " Gamma (k=1,l=1) : " << RandGamma::shoot() << endl; 00079 cout << " Gamma (k=3,l=0.5) : " << RandGamma::shoot(3,0.5) << endl; 00080 cout << " StudT (a=1) : " << RandStudentT::shoot() << endl; 00081 cout << " StudT (a=2.5) : " << RandStudentT::shoot(2.5) << endl; 00082 cout << endl; 00083 cout << " Shooting an array of 5 flat numbers ..." << endl << endl; 00084 RandFlat::shootArray(size,vect); 00085 for ( int i=0; i<size; ++i ) 00086 cout << " " << vect[i]; 00087 cout << endl << endl; 00088 } // end layout() 00089 00090 void dist_layout() 00091 { 00092 float m1=3.0; 00093 const int size=5; 00094 double vect[size]; 00095 00096 HepJamesRandom aJamesEngine; 00097 RandEngine aRandEngine; 00098 DRand48Engine aDRand48Engine; 00099 RanluxEngine aRanluxEngine(19780503,4); 00100 Ranlux64Engine aRanlux64Engine; 00101 RanecuEngine aRanecuEngine; 00102 Hurd288Engine aHurd288Engine; 00103 MTwistEngine aMTwistEngine; 00104 RanshiEngine aRanshiEngine; 00105 TripleRand aTripleRandEngine; 00106 00107 RandFlat aFlatObj(aJamesEngine); 00108 RandExponential anExponentialObj(aRandEngine); 00109 RandGauss aGaussObj(aDRand48Engine); 00110 RandBreitWigner aBreitObj(aRanluxEngine); 00111 RandPoisson aPoissonObj(aRanecuEngine); 00112 RandBinomial aBinomialObj(aHurd288Engine); 00113 RandChiSquare aChiSquareObj(aMTwistEngine); 00114 RandGamma aGammaObj(aRanshiEngine); 00115 RandStudentT aStudentTObj(aTripleRandEngine); 00116 00117 cout << " ----- Press <ENTER> to continue -----"; 00118 if ( cin.get() != '\n') exit(0); 00119 cout << endl << endl; 00120 cout << "-------------------- Shooting test on distribution objects --------------------" << endl; 00121 cout << endl; 00122 cout << " Flat ]0,1[ : " << aFlatObj.fire() << endl; 00123 cout << " Flat ]0,5[ : " << aFlatObj.fire(5) << endl; 00124 cout << " Flat ]-5,3[ : " << aFlatObj.fire(-5,3) << endl; 00125 cout << " Exp (m=1) : " << anExponentialObj.fire() << endl; 00126 cout << " Exp (m=3) : " << anExponentialObj.fire(3) << endl; 00127 cout << " Gauss (m=1) : " << aGaussObj.fire() << endl; 00128 cout << " Gauss (m=3,v=1) : " << aGaussObj.fire(3,1) << endl; 00129 cout << " Wigner(1,0.2) : " << aBreitObj.fire(1,0.2) << endl; 00130 cout << " Wigner(1,0.2,1) : " << aBreitObj.fire(1,0.2,1) << endl; 00131 cout << " Wigner2(1,0.2) : " << aBreitObj.fireM2(1,0.2) << endl; 00132 cout << " Wigner2(1,0.2,1) : " << aBreitObj.fireM2(1,0.2,1) << endl; 00133 cout << " IntFlat [0,99[ : " << aFlatObj.fireInt(99) << endl; 00134 cout << " IntFlat [-99,37[ : " << aFlatObj.fireInt(-99,37) << endl; 00135 cout << " Poisson (m=3.0) : " << aPoissonObj.fire(m1) << endl; 00136 cout << " Binomial(n=1,p=0.5) : " << aBinomialObj.fire() << endl; 00137 cout << " Binomial(n=-5,p=0.3): " << aBinomialObj.fire(-5,0.3) << endl; 00138 cout << " ChiSqr (a=1) : " << aChiSquareObj.fire() << endl; 00139 cout << " ChiSqr (a=-5) : " << aChiSquareObj.fire(-5) << endl; 00140 cout << " Gamma (k=1,l=1) : " << aGammaObj.fire() << endl; 00141 cout << " Gamma (k=3,l=0.5) : " << aGammaObj.fire(3,0.5) << endl; 00142 cout << " StudT (a=1) : " << aStudentTObj.fire() << endl; 00143 cout << " StudT (a=2.5) : " << aStudentTObj.fire(2.5) << endl; 00144 cout << endl; 00145 cout << " Shooting an array of 5 flat numbers ..." << endl << endl; 00146 aFlatObj.fireArray(size,vect); 00147 for ( int i=0; i<size; ++i ) 00148 cout << " " << vect[i]; 00149 cout << endl << endl; 00150 cout << " ----- Press <ENTER> to continue -----"; 00151 if ( cin.get() != '\n') exit(0); 00152 } // end dist_layout() 00153 00154 void user_layout() 00155 { 00156 float m1=3.0; 00157 const int size=5; 00158 double vect[size]; 00159 char sel; 00160 HepRandomEngine* anEngine; 00161 00162 cout << endl << endl; 00163 cout << "-------------------- Shooting test skeeping the generator ---------------------" << endl; 00164 cout << endl; 00165 cout << " >>> Select a Random Engine <<<" << endl << endl; 00166 cout << " a. HepJamesRandom (default)" << endl; 00167 cout << " b. Rand" << endl; 00168 cout << " c. DRand48" << endl; 00169 cout << " d. Ranlux" << endl; 00170 cout << " e. Ranlux64" << endl; 00171 cout << " f. Ranecu" << endl; 00172 cout << " g. Hurd160" << endl; 00173 cout << " h. Hurd288" << endl; 00174 cout << " i. MTwist" << endl; 00175 cout << " j. Ranshi" << endl; 00176 cout << " k. DualRand" << endl; 00177 cout << " l. TripleRand" << endl << endl; 00178 cout << " > "; 00179 cin >> sel; 00180 while ((sel!='a')&&(sel!='b')&&(sel!='c')&&(sel!='d')&&(sel!='e')&& 00181 (sel!='f')&&(sel!='g')&&(sel!='h')&&(sel!='i')&&(sel!='j')&& 00182 (sel!='k')&&(sel!='l')) { 00183 cout << endl << " >>> Choice not legal !! [a..l]<<<" << endl; 00184 cin >> sel; 00185 } 00186 00187 switch (sel) { 00188 case 'a': 00189 anEngine = &theJamesEngine; 00190 break; 00191 case 'b': 00192 anEngine = &theRandEngine; 00193 break; 00194 case 'c': 00195 anEngine = &theDRand48Engine; 00196 break; 00197 case 'd': 00198 anEngine = &theRanluxEngine; 00199 break; 00200 case 'e': 00201 anEngine = &theRanlux64Engine; 00202 break; 00203 case 'f': 00204 anEngine = &theRanecuEngine; 00205 break; 00206 case 'g': 00207 anEngine = &theHurd160Engine; 00208 break; 00209 case 'h': 00210 anEngine = &theHurd288Engine; 00211 break; 00212 case 'i': 00213 anEngine = &theMTwistEngine; 00214 break; 00215 case 'j': 00216 anEngine = &theRanshiEngine; 00217 break; 00218 case 'k': 00219 anEngine = &theDualRandEngine; 00220 break; 00221 case 'l': 00222 anEngine = &theTripleRandEngine; 00223 break; 00224 default: 00225 anEngine = &theJamesEngine; 00226 break; 00227 } 00228 cout << endl; 00229 00230 cout << " Flat ]0,1[ : " << RandFlat::shoot(anEngine) << endl; 00231 cout << " Flat ]0,5[ : " << RandFlat::shoot(anEngine,5) << endl; 00232 cout << " Flat ]-5,3[ : " << RandFlat::shoot(anEngine,-5,3) << endl; 00233 cout << " Exp (m=1) : " << RandExponential::shoot(anEngine) << endl; 00234 cout << " Exp (m=3) : " << RandExponential::shoot(anEngine,3) << endl; 00235 cout << " Gauss (m=1) : " << RandGauss::shoot(anEngine) << endl; 00236 cout << " Gauss (m=3,v=1) : " << RandGauss::shoot(anEngine,3,1) << endl; 00237 cout << " Wigner(1,0.2) : " << RandBreitWigner::shoot(anEngine,1,0.2) << endl; 00238 cout << " Wigner(1,0.2,1) : " << RandBreitWigner::shoot(anEngine,1,0.2,1) << endl; 00239 cout << " Wigner2(1,0.2) : " << RandBreitWigner::shootM2(anEngine,1,0.2) << endl; 00240 cout << " Wigner2(1,0.2,1) : " << RandBreitWigner::shootM2(anEngine,1,0.2,1) << endl; 00241 cout << " IntFlat [0,99[ : " << RandFlat::shootInt(anEngine,99) << endl; 00242 cout << " IntFlat [-99,37[ : " << RandFlat::shootInt(anEngine,-99,37) << endl; 00243 cout << " Poisson (m=3.0) : " << RandPoisson::shoot(anEngine,m1) << endl; 00244 cout << " Binomial(n=1,p=0.5) : " << RandBinomial::shoot(anEngine) << endl; 00245 cout << " Binomial(n=-5,p=0.3): " << RandBinomial::shoot(anEngine,-5,0.3) << endl; 00246 cout << " ChiSqr (a=1) : " << RandChiSquare::shoot(anEngine) << endl; 00247 cout << " ChiSqr (a=-5) : " << RandChiSquare::shoot(anEngine,-5) << endl; 00248 cout << " Gamma (k=1,l=1) : " << RandGamma::shoot(anEngine) << endl; 00249 cout << " Gamma (k=3,l=0.5) : " << RandGamma::shoot(anEngine,3,0.5) << endl; 00250 cout << " StudT (a=1) : " << RandStudentT::shoot(anEngine) << endl; 00251 cout << " StudT (a=2.5) : " << RandStudentT::shoot(anEngine,2.5) << endl; 00252 cout << endl; 00253 cout << " Shooting an array of 5 flat numbers ..." << endl << endl; 00254 RandFlat::shootArray(anEngine,size,vect); 00255 for ( int i=0; i<size; ++i ) 00256 cout << " " << vect[i]; 00257 cout << endl << endl; 00258 } // end layout() 00259 00260 void start_test() 00261 { 00262 cout << "------------------------- Test on HepJamesRandom ----------------------------" << endl; 00263 cout << endl; 00264 layout(); 00265 cout << " ----- Press <ENTER> to continue -----"; 00266 if ( cin.get() != '\n') exit(0); 00267 cout << endl; 00268 cout << "--------------------------- Test on RandEngine ------------------------------" << endl; 00269 cout << endl; 00270 HepRandom::setTheEngine(&theRandEngine); 00271 layout(); 00272 cout << " ----- Press <ENTER> to continue -----"; 00273 if ( cin.get() != '\n') exit(0); 00274 cout << endl; 00275 cout << "------------------------- Test on DRand48Engine -----------------------------" << endl; 00276 cout << endl; 00277 HepRandom::setTheEngine(&theDRand48Engine); 00278 layout(); 00279 cout << " ----- Press <ENTER> to continue -----"; 00280 if ( cin.get() != '\n') exit(0); 00281 cout << endl; 00282 cout << "--------------------- Test on RanluxEngine (luxury 4) ------------------------" << endl; 00283 cout << endl; 00284 HepRandom::setTheEngine(&theRanluxEngine); 00285 layout(); 00286 cout << " ----- Press <ENTER> to continue -----"; 00287 if ( cin.get() != '\n') exit(0); 00288 cout << endl; 00289 cout << "------------------------- Test on Ranlux64Engine -----------------------------" << endl; 00290 cout << endl; 00291 HepRandom::setTheEngine(&theRanlux64Engine); 00292 layout(); 00293 cout << " ----- Press <ENTER> to continue -----"; 00294 if ( cin.get() != '\n') exit(0); 00295 cout << endl; 00296 cout << "-------------------------- Test on RanecuEngine ------------------------------" << endl; 00297 cout << endl; 00298 HepRandom::setTheEngine(&theRanecuEngine); 00299 layout(); 00300 cout << " ----- Press <ENTER> to continue -----"; 00301 if ( cin.get() != '\n') exit(0); 00302 cout << endl; 00303 cout << "------------------------- Test on Hurd160Engine ------------------------------" << endl; 00304 cout << endl; 00305 HepRandom::setTheEngine(&theHurd160Engine); 00306 layout(); 00307 cout << " ----- Press <ENTER> to continue -----"; 00308 if ( cin.get() != '\n') exit(0); 00309 cout << endl; 00310 cout << "------------------------- Test on Hurd288Engine ------------------------------" << endl; 00311 cout << endl; 00312 HepRandom::setTheEngine(&theHurd288Engine); 00313 layout(); 00314 cout << " ----- Press <ENTER> to continue -----"; 00315 if ( cin.get() != '\n') exit(0); 00316 cout << endl; 00317 cout << "-------------------------- Test on MTwistEngine ------------------------------" << endl; 00318 cout << endl; 00319 HepRandom::setTheEngine(&theMTwistEngine); 00320 layout(); 00321 cout << " ----- Press <ENTER> to continue -----"; 00322 if ( cin.get() != '\n') exit(0); 00323 cout << endl; 00324 cout << "-------------------------- Test on RanshiEngine ------------------------------" << endl; 00325 cout << endl; 00326 HepRandom::setTheEngine(&theRanshiEngine); 00327 layout(); 00328 cout << " ----- Press <ENTER> to continue -----"; 00329 if ( cin.get() != '\n') exit(0); 00330 cout << endl; 00331 cout << "------------------------- Test on DualRandEngine -----------------------------" << endl; 00332 cout << endl; 00333 HepRandom::setTheEngine(&theDualRandEngine); 00334 layout(); 00335 cout << " ----- Press <ENTER> to continue -----"; 00336 if ( cin.get() != '\n') exit(0); 00337 cout << endl; 00338 cout << "------------------------ Test on TripleRandEngine ----------------------------" << endl; 00339 cout << endl; 00340 HepRandom::setTheEngine(&theTripleRandEngine); 00341 layout(); 00342 dist_layout(); 00343 user_layout(); 00344 } // end start_test() 00345 00346 00347 int main() { 00348 00349 init(); 00350 start_test(); 00351 00352 return 0; 00353 } 00354