13 #include "CLHEP/Random/RanecuEngine.h"
14 #include "CLHEP/Random/Random.h"
22 std::vector<double> ab(2);
27 bool printCheck(
int & i,
double & r, std::ofstream & os )
29 os << i <<
" " << r << std::endl;
30 if (r < 0 || r > 1.0 ) {
31 std::cout <<
"Error: bad random number " << r << std::endl;
39 std::ofstream
output(
"testBug58950.cout");
41 output << std::endl <<
"short " <<
sizeof(short) << std::endl;
42 output <<
"int " <<
sizeof(int) << std::endl;
43 output <<
"unsigned int " <<
sizeof(
unsigned int) << std::endl;
44 output <<
"long " <<
sizeof(long) << std::endl;
45 output <<
"float " <<
sizeof(float) << std::endl;
46 output <<
"double " <<
sizeof(
double) << std::endl;
47 output <<
"long double " <<
sizeof(
long double) << std::endl << std::endl;
56 std::ifstream in(
"/dev/urandom", std::ios::in | std::ios::binary);
58 in.read((
char *)(&rvals), 2*
sizeof(
long));
61 throw std::runtime_error(
"File read error");
63 }
else throw std::runtime_error(
"File open error");
64 }
catch(std::runtime_error e) {
65 std::ostringstream dStr;
66 dStr <<
"Error: " << e.what()
67 <<
" processing seed from file \"" <<
"/dev/urandom" <<
"\".";
68 throw std::runtime_error(dStr.str().c_str());
83 if( rvals[0] > 0 ) seeds[0] = -rvals[0];
85 double negseq[20] = { 0.154707, 0.587114, 0.702059, 0.566, 0.988325,
86 0.525921, 0.191554, 0.269338, 0.234277, 0.358997,
87 0.549936, 0.296877, 0.162243, 0.227732, 0.528862,
88 0.631571, 0.176462, 0.247858, 0.170025, 0.284483 };
91 output << std::endl <<
"********************" << std::endl;
92 output <<
"This is the case that may or may not fail." << std::endl;
93 output <<
"However, if it has values in (0,1), they are a " << std::endl
94 <<
"deterministic sequence beginning with 0.154707." << std::endl;
95 output <<
"seeds[0] = " << seeds[0] <<
"\n"
96 <<
"seeds[1] = " << seeds[1] << std::endl << std::endl;
100 for (
int i=0; i < nNumbers; ++i) {
101 double r = g->
flat();
104 if( std::fabs(r-negseq[i]) < eps ) {
105 std::cout <<
" reproducing sequence " << i <<
" "
106 << r <<
" " << negseq[i] << std::endl;
110 if( rseq == 20 ) ++badcount;
112 output <<
"Final seeds[0] = " << pseeds[0] <<
"\n"
113 <<
"Final seeds[1] = " << pseeds[1] << std::endl << std::endl;
117 seeds[0]=labs(rvals[0]);
118 seeds[1]=labs(rvals[1]);
121 output << std::endl <<
"********************" << std::endl;
122 output <<
"This is the case that always fails." << std::endl;
123 output <<
"seeds[0] = " << seeds[0] <<
"\n"
124 <<
"seeds[1] = " << seeds[1] << std::endl << std::endl;
127 for (
int i=0; i < nNumbers; ++i) {
128 double r = g->
flat();
132 output <<
"Final seeds[0] = " << pseeds[0] <<
"\n"
133 <<
"Final seeds[1] = " << pseeds[1] << std::endl << std::endl;
137 seeds[0]=labs(rvals[0]);
138 seeds[1]=labs(rvals[1]);
141 output << std::endl <<
"********************" << std::endl;
142 output <<
"Check rolling back a random number seed." << std::endl;
143 output <<
"seeds[0] = " << seeds[0] <<
"\n"
144 <<
"seeds[1] = " << seeds[1] << std::endl << std::endl;
145 std::vector<double> v;
148 for (
int i=0; i < nNumbers; ++i) {
149 double r = g->
flat();
153 seeds[0] = pseeds[0];
154 seeds[1] = pseeds[1];
155 output <<
" pseeds[0] = " << pseeds[0] <<
"\n"
156 <<
"pseeds[1] = " << pseeds[1] << std::endl;
157 for (
int i=0; i < nNumbers; ++i) {
158 double r = g->
flat();
162 for (
int i=0; i < nNumbers; ++i) {
163 double r = g->
flat();
166 std::cerr <<
" rollback fails: i, v[i], r "
167 << i <<
" " << v[i] <<
" " << r << std::endl;
174 seeds[0]= labs(static_cast<int>(rvals[0]));
175 seeds[1]= labs(static_cast<int>(rvals[1]));
178 output << std::endl <<
"********************" << std::endl;
179 output <<
"This is the case that works." << std::endl;
180 output << std::endl <<
"seeds[0] = " << seeds[0] <<
"\n"
181 <<
"seeds[1] = " << seeds[1] <<
"\n"
182 <<
"seeds[2] = " << seeds[2] << std::endl << std::endl;
185 for (
int i=0; i < nNumbers; ++i) {
186 double r = g->
flat();
190 output <<
"Final seeds[0] = " << pseeds[0] <<
"\n"
191 <<
"Final seeds[1] = " << pseeds[1] << std::endl << std::endl;
198 seeds[0]=labs(rvals[0]);
199 seeds[1]=labs(rvals[1]);
202 output << std::endl <<
"********************" << std::endl;
203 output <<
"This case loops until valid short seeds occur." << std::endl;
204 output <<
"seeds[0] = " << seeds[0] <<
"\n"
205 <<
"seeds[1] = " << seeds[1] << std::endl << std::endl;
210 unsigned int low = ~0;
211 unsigned long mask = (~0) << 31;
212 unsigned long skipcount = 0;
213 output <<
"low = " << low <<
" mask = " << mask << std::endl;
219 }
while((pseeds[0]&mask) || (pseeds[1]&mask));
220 if ( skipcount > 1 ) ++badcount;
222 output << std::endl <<
"Loop terminates on two short seeds." << std::endl;
223 output <<
"Skipcount = " << skipcount << std::endl;
224 output <<
"pseeds[0]&mask = " << (pseeds[0]&mask) << std::endl;
225 output <<
"pseeds[1]&mask = " << (pseeds[1]&mask) << std::endl;
226 output <<
"Final seeds[0] = " << pseeds[0] <<
"\n"
227 <<
"Final seeds[1] = " << pseeds[1] << std::endl << std::endl;
229 output <<
"This should be a valid sequence." << std::endl;
230 for (
int i=0; i < nNumbers; ++i) {
231 double r1 = g->
flat();
235 output <<
"seeds[0] = " << pseeds[0] <<
"\n"
236 <<
"seeds[1] = " << pseeds[1] << std::endl << std::endl;
238 if( badcount > 0 ) std::cout <<
"Error count is " << badcount << std::endl;
std::ofstream output("ranRestoreTest.cout")
static const long * getTheSeeds()
bool equals(double a, double b)
static HepRandom * getTheGenerator()
bool equals01(const std::vector< double > &ab)
void pretend_to_use(T const &)
static void setTheEngine(HepRandomEngine *theNewEngine)
bool printCheck(int &i, double &r, std::ofstream &os)
static void setTheSeeds(const long *seeds, int aux=-1)