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

testRanecuSequence.cc

Go to the documentation of this file.
00001 // ----------------------------------------------------------------------
00002 //
00003 // testRanecuSequence 
00004 // look at report that certain seeds produce identical sequences
00005 // 
00006 // ----------------------------------------------------------------------
00007 #include <iostream> 
00008 #include <string>
00009 #include <vector>
00010 #include "CLHEP/Units/GlobalPhysicalConstants.h"  // used to provoke shadowing warnings
00011 #include "CLHEP/Random/RanecuEngine.h"
00012 #include "CLHEP/Random/Random.h"
00013 
00014 struct Sample {
00015     int seed;
00016     std::vector<double> case1;
00017     std::vector<double> case2;
00018     std::vector<double> case3;
00019     std::vector<double> case4;
00020     std::vector<double> case5;
00021 };
00022 
00023 void useSeed( int, std::vector<Sample>& );
00024 
00025 bool compareSamples( Sample&, Sample&, std::ofstream& );
00026 
00027 int main() { 
00028 
00029     std::ofstream output("testRanecuSequence.output");  
00030 
00031     output << " Setting CLHEP Random Engine..." << std::endl;
00032     CLHEP::HepRandom::setTheEngine(new CLHEP::RanecuEngine);
00033     
00034     std::vector<Sample> ranList;
00035 
00036     // the first 4 seeds once resulted in identical sequences
00037     useSeed(1275177715, ranList);
00038     useSeed(1275171265, ranList);
00039     useSeed(1275168040, ranList);
00040     useSeed(1275168040-2048*215, ranList);
00041     useSeed(4132429, ranList);
00042     useSeed(-1357924, ranList);
00043     
00044     int sd = 53208134;
00045     for ( int i = 0; i < 1000; ++i ) {
00046         ++sd;
00047         useSeed(sd, ranList);
00048     }
00049     
00050     int numbad = 0;
00051     output << std::endl;
00052     for ( unsigned int it=0; it < ranList.size(); ++it ) {
00053         for ( unsigned int jt=it+1; jt < ranList.size(); ++jt ) {
00054             if( ! compareSamples(ranList[it], ranList[jt], output ) ) {
00055                 ++numbad;
00056                 output << "ERROR: Seed " << ranList[it].seed 
00057                        << " and Seed " << ranList[jt].seed 
00058                        << " are " << (ranList[jt].seed - ranList[it].seed )
00059                        << " apart and result in identical sequences" << std::endl;
00060                 std::cerr << "Seed " << ranList[it].seed 
00061                           << " and Seed " << ranList[jt].seed 
00062                           << " are " << (ranList[jt].seed - ranList[it].seed )
00063                           << " apart and result in identical sequences" << std::endl;
00064             }
00065         }
00066     }
00067 
00068     output << " numbad is " << numbad << std::endl;
00069     return numbad;
00070 } 
00071 
00072 void useSeed( int seed, std::vector<Sample>& ranList )
00073 {
00074     Sample v;
00075     v.seed = seed;
00076 // case 1  -- default constructor
00077     CLHEP::RanecuEngine c1;
00078     for( int i = 0; i < 15; ++i ) {
00079         v.case1.push_back( c1.flat() );
00080     }
00081 // case 2
00082     CLHEP::RanecuEngine c2(seed);
00083     for( int i = 0; i < 15; ++i ) {
00084         v.case2.push_back( c2.flat() );
00085     }
00086 // case 3 - use HepRandom
00087     CLHEP::HepRandom::setTheSeed(seed);
00088     for( int i = 0; i < 15; ++i ) {
00089         v.case3.push_back(  CLHEP::HepRandom::getTheEngine()->flat() );
00090     }
00091 // case 4
00092     CLHEP::RanecuEngine c4(1);
00093     c4.setSeed(seed);
00094     for( int i = 0; i < 15; ++i ) {
00095         v.case4.push_back( c4.flat() );
00096     }
00097 // case 5
00098     CLHEP::RanecuEngine c5(1);
00099     long seedarray[2];
00100     seedarray[0] = seed;
00101     seedarray[1] = 1;
00102     c5.setSeeds(seedarray,2);
00103     for( int i = 0; i < 15; ++i ) {
00104         v.case5.push_back( c5.flat() );
00105     }
00106 //
00107     ranList.push_back(v);
00108 
00109 }
00110 
00111 bool compareSamples( Sample& s1, Sample& s2, std::ofstream& output )
00112 {
00113     if ( s1.case1 == s2.case1 ) {
00114         output << " case1:  default constructor \n" ;
00115         output << " comparing Seed " << s1.seed << " and Seed " << s2.seed << std::endl;
00116         output << " case1 sequence:" ;
00117         for( unsigned int i=0; i < s1.case1.size(); ++i ) output << " " << s1.case1[i];
00118         output << std::endl;
00119         return false;
00120     }
00121     if ( s1.case2 == s2.case2 ) {
00122         output << " case2:  construct with single seed \n" ;
00123         output << " comparing Seed " << s1.seed << " and Seed " << s2.seed << std::endl;
00124         output << " case2 sequence:" ;
00125         for( unsigned int i=0; i < s1.case2.size(); ++i ) output << " " << s1.case2[i];
00126         output << std::endl;
00127         return false;
00128     }
00129     if ( s1.case3 == s2.case3 ) {
00130         output << " case3:  construct with single seed \n" ;
00131         output << " comparing Seed " << s1.seed << " and Seed " << s2.seed << std::endl;
00132         output << " case3 sequence:" ;
00133         for( unsigned int i=0; i < s1.case3.size(); ++i ) output << " " << s1.case3[i];
00134         output << std::endl;
00135         return false;
00136     }
00137     if ( s1.case4 == s2.case4 ) {
00138         output << " case4:  use setSeed \n" ;
00139         output << " comparing Seed " << s1.seed << " and Seed " << s2.seed << std::endl;
00140         output << " case4 sequence:" ;
00141         for( unsigned int i=0; i < s1.case4.size(); ++i ) output << " " << s1.case4[i];
00142         output << std::endl;
00143         return false;
00144     }
00145     if ( s1.case5 == s2.case5 ) {
00146         output << " case5:  use setSeeds \n" ;
00147         output << " comparing Seed " << s1.seed << " and Seed " << s2.seed << std::endl;
00148         output << " case5 sequence:" ;
00149         for( unsigned int i=0; i < s1.case5.size(); ++i ) output << " " << s1.case5[i];
00150         output << std::endl;
00151         return false;
00152     }
00153 
00154     return true;
00155 }

Generated on 15 Nov 2012 for CLHEP by  doxygen 1.4.7