41 #include "CLHEP/Random/defs.h"
42 #include "CLHEP/Random/RandEngine.h"
43 #include "CLHEP/Random/Random.h"
44 #include "CLHEP/Random/engineIDulong.h"
57 #define RAND_MAX 9382956
58 #include "CLHEP/Random/MTwistEngine.h"
59 #include "CLHEP/Random/RandFlat.h"
60 MTwistEngine * fakeFlat =
new MTwistEngine;
61 RandFlat rflat (fakeFlat, 0, RAND_MAX+1);
62 int rand() {
return (
int)rflat(); }
65 static const int MarkerLen = 64;
68 int RandEngine::numEngines = 0;
71 int RandEngine::maxIndex = 215;
89 int cycle = std::abs(
int(rowIndex/maxIndex));
90 int row = std::abs(
int(rowIndex%maxIndex));
91 int col = std::abs(
int(colIndex%2));
92 long mask = ((cycle & 0x000007ff) << 20 );
94 seed = (seeds[col])^mask;
107 cycle = std::abs(
int(numEngines/maxIndex));
108 curIndex = std::abs(
int(numEngines%maxIndex));
110 long mask = ((cycle & 0x007fffff) << 8);
112 seed = seeds[0]^mask;
135 setSeed(seeds ? *seeds : 19780503L, 0);
141 std::ofstream outFile( filename, std::ios::out ) ;
143 if (!outFile.bad()) {
145 std::vector<unsigned long> v =
put();
147 std::cout <<
"Result of v = put() is:\n";
149 for (
unsigned int i=0; i<v.size(); ++i) {
150 outFile << v[i] <<
"\n";
152 std::cout << v[i] <<
" ";
153 if (i%6==0) std::cout <<
"\n";
161 if (!outFile.bad()) {
162 outFile <<
theSeed << std::endl;
163 outFile << seq << std::endl;
175 std::ifstream inFile( filename, std::ios::in);
177 std::cout <<
" -- Engine state remains unchanged\n";
181 std::vector<unsigned long> v;
186 std::cout <<
"ivec = " << ivec <<
" xin = " << xin <<
" ";
187 if (ivec%3 == 0) std::cout <<
"\n";
190 inFile.clear(std::ios::badbit | inFile.rdstate());
191 std::cerr <<
"\nRandEngine state (vector) description improper."
192 <<
"\nrestoreStatus has failed."
193 <<
"\nInput stream is probably mispositioned now." << std::endl;
204 if (!inFile.bad() && !inFile.eof()) {
209 while (seq < count)
flat();
215 std::cout << std::endl;
216 std::cout <<
"---------- Rand engine status ----------" << std::endl;
217 std::cout <<
" Initial seed = " <<
theSeed << std::endl;
218 std::cout <<
" Shooted sequences = " << seq << std::endl;
219 std::cout <<
"----------------------------------------" << std::endl;
244 static bool prepared =
false;
245 static unsigned int iT;
246 static unsigned int iK;
247 static unsigned int iS;
252 if ( (RAND_MAX >> 31) > 0 )
256 iS = (
unsigned long)RAND_MAX + 1;
263 if( (
unsigned long) (RAND_MAX) <= (( (1uL) << 31 ) - 1 ) ) {
269 for ( m = 0; m < 64; ++m ) {
271 if (StoK == 0)
break;
278 for (
int i = 0; i < iK; ++i ) {
279 v = iS*v+rand(); ++seq;
282 return v & 0xFFFFFFFF;
286 else if ( (RAND_MAX >> 26) == 0 )
292 fS = (
unsigned long)RAND_MAX + 1;
293 double twoTo32 = std::ldexp(1.0,32);
295 for ( iK = 1; StoK < twoTo32; StoK *= fS, iK++ ) { }
298 for ( m = 0; m < 64; ++m ) {
300 if (StoK < 1.0)
break;
307 for (
int i = 0; i < iK; ++i ) {
308 v = fS*v+rand(); ++seq;
311 return ((
unsigned int)v) & 0xFFFFFFFF;
319 iS = (
unsigned long)RAND_MAX + 1;
321 for (iN = 0; SshiftN > 1; SshiftN >>= 1, iN++) { }
326 do { x1 = rand(); ++seq;}
while (x1 < (1<<16) );
327 do { x2 = rand(); ++seq;}
while (x2 < (1<<16) );
328 return x1 | (x2 << 16);
336 unsigned int x = rand() << 1; ++seq;
337 x ^= ( (x>>23) ^ (x>>7) ) ^1;
338 return x & 0xFFFFFFFF;
345 unsigned int x = rand() << 17; ++seq;
346 x ^= rand() << 2; ++seq;
347 x ^= ( (x>>23) ^ (x>>7) ) ^3;
348 return x & 0xFFFFFFFF;
357 return r/4294967296.0;
364 for (i=0; i<size; ++i)
368 RandEngine::operator
unsigned int() {
374 char beginMarker[] =
"RandEngine-begin";
375 char endMarker[] =
"RandEngine-end";
377 os <<
" " << beginMarker <<
"\n";
378 os <<
theSeed <<
" " << seq <<
" ";
379 os << endMarker <<
"\n";
384 std::vector<unsigned long> v;
385 v.push_back (engineIDulong<RandEngine>());
386 v.push_back(static_cast<unsigned long>(
theSeed));
387 v.push_back(static_cast<unsigned long>(seq));
397 char beginMarker [MarkerLen];
403 if (strcmp(beginMarker,
"RandEngine-begin")) {
404 is.clear(std::ios::badbit | is.rdstate());
405 std::cout <<
"\nInput stream mispositioned or"
406 <<
"\nRandEngine state description missing or"
407 <<
"\nwrong engine type found." << std::endl;
414 return "RandEngine-begin";
420 std::vector<unsigned long> v;
425 is.clear(std::ios::badbit | is.rdstate());
426 std::cerr <<
"\nRandEngine state (vector) description improper."
427 <<
"\ngetState() has failed."
428 <<
"\nInput stream is probably mispositioned now." << std::endl;
439 char endMarker [MarkerLen];
445 if (strcmp(endMarker,
"RandEngine-end")) {
446 is.clear(std::ios::badbit | is.rdstate());
447 std::cerr <<
"\nRandEngine state description incomplete."
448 <<
"\nInput stream is probably mispositioned now." << std::endl;
452 while (seq < count)
flat();
457 if ((v[0] & 0xffffffffUL) != engineIDulong<RandEngine>()) {
459 "\nRandEngine get:state vector has wrong ID word - state unchanged\n";
468 "\nRandEngine get:state vector has wrong length - state unchanged\n";
474 while (seq < count)
flat();