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
|
// ----------------------------------------------------------------------
//
// testRanecuSequence
// look at report that certain seeds produce identical sequences
//
// ----------------------------------------------------------------------
#include <iostream>
#include <string>
#include <vector>
#include "CLHEP/Units/GlobalPhysicalConstants.h" // used to provoke shadowing warnings
#include "CLHEP/Random/RanecuEngine.h"
#include "CLHEP/Random/Random.h"
struct Sample {
int seed;
std::vector<double> case1;
std::vector<double> case2;
std::vector<double> case3;
std::vector<double> case4;
std::vector<double> case5;
};
void useSeed( int, std::vector<Sample>& );
bool compareSamples( Sample&, Sample&, std::ofstream& );
int main() {
std::ofstream output("testRanecuSequence.output");
output << " Setting CLHEP Random Engine..." << std::endl;
CLHEP::HepRandom::setTheEngine(new CLHEP::RanecuEngine);
std::vector<Sample> ranList;
// the first 4 seeds once resulted in identical sequences
useSeed(1275177715, ranList);
useSeed(1275171265, ranList);
useSeed(1275168040, ranList);
useSeed(1275168040-2048*215, ranList);
useSeed(4132429, ranList);
useSeed(-1357924, ranList);
int sd = 53208134;
for ( int i = 0; i < 1000; ++i ) {
++sd;
useSeed(sd, ranList);
}
int numbad = 0;
output << std::endl;
for ( unsigned int it=0; it < ranList.size(); ++it ) {
for ( unsigned int jt=it+1; jt < ranList.size(); ++jt ) {
if( ! compareSamples(ranList[it], ranList[jt], output ) ) {
++numbad;
output << "ERROR: Seed " << ranList[it].seed
<< " and Seed " << ranList[jt].seed
<< " are " << (ranList[jt].seed - ranList[it].seed )
<< " apart and result in identical sequences" << std::endl;
std::cerr << "Seed " << ranList[it].seed
<< " and Seed " << ranList[jt].seed
<< " are " << (ranList[jt].seed - ranList[it].seed )
<< " apart and result in identical sequences" << std::endl;
}
}
}
output << " numbad is " << numbad << std::endl;
return numbad;
}
void useSeed( int seed, std::vector<Sample>& ranList )
{
Sample v;
v.seed = seed;
// case 1 -- default constructor
CLHEP::RanecuEngine c1;
for( int i = 0; i < 15; ++i ) {
v.case1.push_back( c1.flat() );
}
// case 2
CLHEP::RanecuEngine c2(seed);
for( int i = 0; i < 15; ++i ) {
v.case2.push_back( c2.flat() );
}
// case 3 - use HepRandom
CLHEP::HepRandom::setTheSeed(seed);
for( int i = 0; i < 15; ++i ) {
v.case3.push_back( CLHEP::HepRandom::getTheEngine()->flat() );
}
// case 4
CLHEP::RanecuEngine c4(1);
c4.setSeed(seed);
for( int i = 0; i < 15; ++i ) {
v.case4.push_back( c4.flat() );
}
// case 5
CLHEP::RanecuEngine c5(1);
long seedarray[2];
seedarray[0] = seed;
seedarray[1] = 1;
c5.setSeeds(seedarray,2);
for( int i = 0; i < 15; ++i ) {
v.case5.push_back( c5.flat() );
}
//
ranList.push_back(v);
}
bool compareSamples( Sample& s1, Sample& s2, std::ofstream& output )
{
if ( s1.case1 == s2.case1 ) {
output << " case1: default constructor \n" ;
output << " comparing Seed " << s1.seed << " and Seed " << s2.seed << std::endl;
output << " case1 sequence:" ;
for( unsigned int i=0; i < s1.case1.size(); ++i ) output << " " << s1.case1[i];
output << std::endl;
return false;
}
if ( s1.case2 == s2.case2 ) {
output << " case2: construct with single seed \n" ;
output << " comparing Seed " << s1.seed << " and Seed " << s2.seed << std::endl;
output << " case2 sequence:" ;
for( unsigned int i=0; i < s1.case2.size(); ++i ) output << " " << s1.case2[i];
output << std::endl;
return false;
}
if ( s1.case3 == s2.case3 ) {
output << " case3: construct with single seed \n" ;
output << " comparing Seed " << s1.seed << " and Seed " << s2.seed << std::endl;
output << " case3 sequence:" ;
for( unsigned int i=0; i < s1.case3.size(); ++i ) output << " " << s1.case3[i];
output << std::endl;
return false;
}
if ( s1.case4 == s2.case4 ) {
output << " case4: use setSeed \n" ;
output << " comparing Seed " << s1.seed << " and Seed " << s2.seed << std::endl;
output << " case4 sequence:" ;
for( unsigned int i=0; i < s1.case4.size(); ++i ) output << " " << s1.case4[i];
output << std::endl;
return false;
}
if ( s1.case5 == s2.case5 ) {
output << " case5: use setSeeds \n" ;
output << " comparing Seed " << s1.seed << " and Seed " << s2.seed << std::endl;
output << " case5 sequence:" ;
for( unsigned int i=0; i < s1.case5.size(); ++i ) output << " " << s1.case5[i];
output << std::endl;
return false;
}
return true;
}
|