CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
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
14struct 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
23void useSeed( int, std::vector<Sample>& );
24
25bool compareSamples( Sample&, Sample&, std::ofstream& );
26
27int 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
72void 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
93 c4.setSeed(seed);
94 for( int i = 0; i < 15; ++i ) {
95 v.case4.push_back( c4.flat() );
96 }
97// case 5
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
111bool 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}
static HepRandomEngine * getTheEngine()
Definition: Random.cc:270
static void setTheSeed(long seed, int lxr=3)
Definition: Random.cc:236
static void setTheEngine(HepRandomEngine *theNewEngine)
Definition: Random.cc:275
void setSeed(long index, int dum=0)
void setSeeds(const long *seeds, int index=-1)
std::ofstream output("ranRestoreTest.cout")
std::vector< double > case5
std::vector< double > case3
std::vector< double > case4
std::vector< double > case2
std::vector< double > case1
bool compareSamples(Sample &, Sample &, std::ofstream &)
int main()
void useSeed(int, std::vector< Sample > &)