13 #include "CLHEP/Random/RanecuEngine.h"
14 #include "CLHEP/Random/Random.h"
17 bool printCheck(
int & i,
double & r, std::ofstream & os )
19 os << i <<
" " << r << std::endl;
20 if (r < 0 || r > 1.0 ) {
21 std::cout <<
"Error: bad random number " << r << std::endl;
29 std::ofstream
output(
"testBug58950.cout");
31 output << std::endl <<
"short " <<
sizeof(short) << std::endl;
32 output <<
"int " <<
sizeof(int) << std::endl;
33 output <<
"unsigned int " <<
sizeof(
unsigned int) << std::endl;
34 output <<
"long " <<
sizeof(long) << std::endl;
35 output <<
"float " <<
sizeof(float) << std::endl;
36 output <<
"double " <<
sizeof(
double) << std::endl;
37 output <<
"long double " <<
sizeof(
long double) << std::endl << std::endl;
46 std::ifstream in(
"/dev/urandom", std::ios::in | std::ios::binary);
48 in.read((
char *)(&rvals), 2*
sizeof(
long));
51 throw std::runtime_error(
"File read error");
53 }
else throw std::runtime_error(
"File open error");
54 }
catch(std::runtime_error e) {
55 std::ostringstream dStr;
56 dStr <<
"Error: " << e.what()
57 <<
" processing seed from file \"" <<
"/dev/urandom" <<
"\".";
58 throw std::runtime_error(dStr.str().c_str());
73 if( rvals[0] > 0 ) seeds[0] = -rvals[0];
75 double negseq[20] = { 0.154707, 0.587114, 0.702059, 0.566, 0.988325,
76 0.525921, 0.191554, 0.269338, 0.234277, 0.358997,
77 0.549936, 0.296877, 0.162243, 0.227732, 0.528862,
78 0.631571, 0.176462, 0.247858, 0.170025, 0.284483 };
81 output << std::endl <<
"********************" << std::endl;
82 output <<
"This is the case that may or may not fail." << std::endl;
83 output <<
"However, if it has values in (0,1), they are a " << std::endl
84 <<
"deterministic sequence beginning with 0.154707." << std::endl;
85 output <<
"seeds[0] = " << seeds[0] <<
"\n"
86 <<
"seeds[1] = " << seeds[1] << std::endl << std::endl;
90 for (
int i=0; i < nNumbers; ++i) {
94 if( std::fabs(r-negseq[i]) < eps ) {
95 std::cout <<
" reproducing sequence " << i <<
" "
96 << r <<
" " << negseq[i] << std::endl;
100 if( rseq == 20 ) ++badcount;
102 output <<
"Final seeds[0] = " << pseeds[0] <<
"\n"
103 <<
"Final seeds[1] = " << pseeds[1] << std::endl << std::endl;
107 seeds[0]=labs(rvals[0]);
108 seeds[1]=labs(rvals[1]);
111 output << std::endl <<
"********************" << std::endl;
112 output <<
"This is the case that always fails." << std::endl;
113 output <<
"seeds[0] = " << seeds[0] <<
"\n"
114 <<
"seeds[1] = " << seeds[1] << std::endl << std::endl;
117 for (
int i=0; i < nNumbers; ++i) {
118 double r = g->
flat();
122 output <<
"Final seeds[0] = " << pseeds[0] <<
"\n"
123 <<
"Final seeds[1] = " << pseeds[1] << std::endl << std::endl;
127 seeds[0]=labs(rvals[0]);
128 seeds[1]=labs(rvals[1]);
131 output << std::endl <<
"********************" << std::endl;
132 output <<
"Check rolling back a random number seed." << std::endl;
133 output <<
"seeds[0] = " << seeds[0] <<
"\n"
134 <<
"seeds[1] = " << seeds[1] << std::endl << std::endl;
135 std::vector<double> v;
138 for (
int i=0; i < nNumbers; ++i) {
139 double r = g->
flat();
143 seeds[0] = pseeds[0];
144 seeds[1] = pseeds[1];
145 output <<
" pseeds[0] = " << pseeds[0] <<
"\n"
146 <<
"pseeds[1] = " << pseeds[1] << std::endl;
147 for (
int i=0; i < nNumbers; ++i) {
148 double r = g->
flat();
152 for (
int i=0; i < nNumbers; ++i) {
153 double r = g->
flat();
156 std::cerr <<
" rollback fails: i, v[i], r "
157 << i <<
" " << v[i] <<
" " << r << std::endl;
164 seeds[0]= labs(static_cast<int>(rvals[0]));
165 seeds[1]= labs(static_cast<int>(rvals[1]));
168 output << std::endl <<
"********************" << std::endl;
169 output <<
"This is the case that works." << std::endl;
170 output << std::endl <<
"seeds[0] = " << seeds[0] <<
"\n"
171 <<
"seeds[1] = " << seeds[1] <<
"\n"
172 <<
"seeds[2] = " << seeds[2] << std::endl << std::endl;
175 for (
int i=0; i < nNumbers; ++i) {
176 double r = g->
flat();
180 output <<
"Final seeds[0] = " << pseeds[0] <<
"\n"
181 <<
"Final seeds[1] = " << pseeds[1] << std::endl << std::endl;
188 seeds[0]=labs(rvals[0]);
189 seeds[1]=labs(rvals[1]);
192 output << std::endl <<
"********************" << std::endl;
193 output <<
"This case loops until valid short seeds occur." << std::endl;
194 output <<
"seeds[0] = " << seeds[0] <<
"\n"
195 <<
"seeds[1] = " << seeds[1] << std::endl << std::endl;
200 unsigned int low = ~0;
201 unsigned long mask = (~0) << 31;
202 unsigned long skipcount = 0;
203 output <<
"low = " << low <<
" mask = " << mask << std::endl;
209 }
while((pseeds[0]&mask) || (pseeds[1]&mask));
210 if ( skipcount > 1 ) ++badcount;
212 output << std::endl <<
"Loop terminates on two short seeds." << std::endl;
213 output <<
"Skipcount = " << skipcount << std::endl;
214 output <<
"pseeds[0]&mask = " << (pseeds[0]&mask) << std::endl;
215 output <<
"pseeds[1]&mask = " << (pseeds[1]&mask) << std::endl;
216 output <<
"Final seeds[0] = " << pseeds[0] <<
"\n"
217 <<
"Final seeds[1] = " << pseeds[1] << std::endl << std::endl;
219 output <<
"This should be a valid sequence." << std::endl;
220 for (
int i=0; i < nNumbers; ++i) {
221 double r1 = g->
flat();
225 output <<
"seeds[0] = " << pseeds[0] <<
"\n"
226 <<
"seeds[1] = " << pseeds[1] << std::endl << std::endl;
228 if( badcount > 0 ) std::cout <<
"Error count is " << badcount << std::endl;