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