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

testRanecuSequence.cc
Go to the documentation of this file.
1 // ----------------------------------------------------------------------
2 //
3 // testRanecuSequence
4 // look at report that certain seeds produce identical sequences
5 //
6 // ----------------------------------------------------------------------
7 #include <iostream>
8 #include <string>
9 #include <vector>
10 #include "CLHEP/Units/GlobalPhysicalConstants.h" // used to provoke shadowing warnings
11 #include "CLHEP/Random/RanecuEngine.h"
12 #include "CLHEP/Random/Random.h"
13 
14 struct Sample {
15  int seed;
16  std::vector<double> case1;
17  std::vector<double> case2;
18  std::vector<double> case3;
19  std::vector<double> case4;
20  std::vector<double> case5;
21 };
22 
23 void useSeed( int, std::vector<Sample>& );
24 
25 bool compareSamples( Sample&, Sample&, std::ofstream& );
26 
27 int main() {
28 
29  std::ofstream output("testRanecuSequence.output");
30 
31  output << " Setting CLHEP Random Engine..." << std::endl;
33 
34  std::vector<Sample> ranList;
35 
36  // the first 4 seeds once resulted in identical sequences
37  useSeed(1275177715, ranList);
38  useSeed(1275171265, ranList);
39  useSeed(1275168040, ranList);
40  useSeed(1275168040-2048*215, ranList);
41  useSeed(4132429, ranList);
42  useSeed(-1357924, ranList);
43 
44  int sd = 53208134;
45  for ( int i = 0; i < 1000; ++i ) {
46  ++sd;
47  useSeed(sd, ranList);
48  }
49 
50  int numbad = 0;
51  output << std::endl;
52  for ( unsigned int it=0; it < ranList.size(); ++it ) {
53  for ( unsigned int jt=it+1; jt < ranList.size(); ++jt ) {
54  if( ! compareSamples(ranList[it], ranList[jt], output ) ) {
55  ++numbad;
56  output << "ERROR: Seed " << ranList[it].seed
57  << " and Seed " << ranList[jt].seed
58  << " are " << (ranList[jt].seed - ranList[it].seed )
59  << " apart and result in identical sequences" << std::endl;
60  std::cerr << "Seed " << ranList[it].seed
61  << " and Seed " << ranList[jt].seed
62  << " are " << (ranList[jt].seed - ranList[it].seed )
63  << " apart and result in identical sequences" << std::endl;
64  }
65  }
66  }
67 
68  output << " numbad is " << numbad << std::endl;
69  return numbad;
70 }
71 
72 void useSeed( int seed, std::vector<Sample>& ranList )
73 {
74  Sample v;
75  v.seed = seed;
76 // case 1 -- default constructor
78  for( int i = 0; i < 15; ++i ) {
79  v.case1.push_back( c1.flat() );
80  }
81 // case 2
82  CLHEP::RanecuEngine c2(seed);
83  for( int i = 0; i < 15; ++i ) {
84  v.case2.push_back( c2.flat() );
85  }
86 // case 3 - use HepRandom
88  for( int i = 0; i < 15; ++i ) {
89  v.case3.push_back( CLHEP::HepRandom::getTheEngine()->flat() );
90  }
91 // case 4
92  CLHEP::RanecuEngine c4(1);
93  c4.setSeed(seed);
94  for( int i = 0; i < 15; ++i ) {
95  v.case4.push_back( c4.flat() );
96  }
97 // case 5
98  CLHEP::RanecuEngine c5(1);
99  long seedarray[2];
100  seedarray[0] = seed;
101  seedarray[1] = 1;
102  c5.setSeeds(seedarray,2);
103  for( int i = 0; i < 15; ++i ) {
104  v.case5.push_back( c5.flat() );
105  }
106 //
107  ranList.push_back(v);
108 
109 }
110 
111 bool compareSamples( Sample& s1, Sample& s2, std::ofstream& output )
112 {
113  if ( s1.case1 == s2.case1 ) {
114  output << " case1: default constructor \n" ;
115  output << " comparing Seed " << s1.seed << " and Seed " << s2.seed << std::endl;
116  output << " case1 sequence:" ;
117  for( unsigned int i=0; i < s1.case1.size(); ++i ) output << " " << s1.case1[i];
118  output << std::endl;
119  return false;
120  }
121  if ( s1.case2 == s2.case2 ) {
122  output << " case2: construct with single seed \n" ;
123  output << " comparing Seed " << s1.seed << " and Seed " << s2.seed << std::endl;
124  output << " case2 sequence:" ;
125  for( unsigned int i=0; i < s1.case2.size(); ++i ) output << " " << s1.case2[i];
126  output << std::endl;
127  return false;
128  }
129  if ( s1.case3 == s2.case3 ) {
130  output << " case3: construct with single seed \n" ;
131  output << " comparing Seed " << s1.seed << " and Seed " << s2.seed << std::endl;
132  output << " case3 sequence:" ;
133  for( unsigned int i=0; i < s1.case3.size(); ++i ) output << " " << s1.case3[i];
134  output << std::endl;
135  return false;
136  }
137  if ( s1.case4 == s2.case4 ) {
138  output << " case4: use setSeed \n" ;
139  output << " comparing Seed " << s1.seed << " and Seed " << s2.seed << std::endl;
140  output << " case4 sequence:" ;
141  for( unsigned int i=0; i < s1.case4.size(); ++i ) output << " " << s1.case4[i];
142  output << std::endl;
143  return false;
144  }
145  if ( s1.case5 == s2.case5 ) {
146  output << " case5: use setSeeds \n" ;
147  output << " comparing Seed " << s1.seed << " and Seed " << s2.seed << std::endl;
148  output << " case5 sequence:" ;
149  for( unsigned int i=0; i < s1.case5.size(); ++i ) output << " " << s1.case5[i];
150  output << std::endl;
151  return false;
152  }
153 
154  return true;
155 }
Sample::case3
std::vector< double > case3
Definition: testRanecuSequence.cc:18
CLHEP::RanecuEngine::setSeed
void setSeed(long index, int dum=0)
Definition: RanecuEngine.cc:106
Sample::case4
std::vector< double > case4
Definition: testRanecuSequence.cc:19
CLHEP::RanecuEngine::flat
double flat()
Definition: RanecuEngine.cc:214
Sample::seed
int seed
Definition: testRanecuSequence.cc:15
CLHEP::RanecuEngine
Definition: Matrix/CLHEP/Random/RanecuEngine.h:48
compareSamples
bool compareSamples(Sample &, Sample &, std::ofstream &)
Definition: testRanecuSequence.cc:111
CLHEP::HepRandom::setTheEngine
static void setTheEngine(HepRandomEngine *theNewEngine)
Definition: Random.cc:171
useSeed
void useSeed(int, std::vector< Sample > &)
Definition: testRanecuSequence.cc:72
Sample
Definition: testRanecuSequence.cc:14
Sample::case5
std::vector< double > case5
Definition: testRanecuSequence.cc:20
v
they are gone ZOOM Features Discontinued The following features of the ZOOM package were felt to be extreme overkill These have been after checking that no existing user code was utilizing as in SpaceVector v
Definition: keyMergeIssues.doc:324
Sample::case2
std::vector< double > case2
Definition: testRanecuSequence.cc:17
output
std::ofstream output("ranRestoreTest.cout")
CLHEP::RanecuEngine::setSeeds
void setSeeds(const long *seeds, int index=-1)
Definition: RanecuEngine.cc:116
i
long i
Definition: JamesRandomSeeding.txt:27
CLHEP::HepRandom::setTheSeed
static void setTheSeed(long seed, int lux=3)
Definition: Random.cc:132
CLHEP::HepRandom::getTheEngine
static HepRandomEngine * getTheEngine()
Definition: Random.cc:166
main
int main()
Definition: testRanecuSequence.cc:27
Sample::case1
std::vector< double > case1
Definition: testRanecuSequence.cc:16