41#include "CLHEP/Random/defs.h"
42#include "CLHEP/Random/RandEngine.h"
43#include "CLHEP/Random/Random.h"
44#include "CLHEP/Random/engineIDulong.h"
61#define RAND_MAX 9382956
62#include "CLHEP/Random/MTwistEngine.h"
63#include "CLHEP/Random/RandFlat.h"
64MTwistEngine * fakeFlat =
new MTwistEngine;
65RandFlat rflat (fakeFlat, 0, RAND_MAX+1);
66int rand() {
return (
int)rflat(); }
69static const int MarkerLen = 64;
72int RandEngine::numEngines = 0;
75const int RandEngine::maxIndex = 215;
93 int cycle = std::abs(
int(rowIndex/maxIndex));
94 int row = std::abs(
int(rowIndex%maxIndex));
95 int col = std::abs(
int(colIndex%2));
96 long mask = ((cycle & 0x000007ff) << 20 );
98 seed = (seeds[col])^mask;
111 cycle = std::abs(
int(numEngines/maxIndex));
112 curIndex = std::abs(
int(numEngines%maxIndex));
114 long mask = ((cycle & 0x007fffff) << 8);
116 seed = seeds[0]^mask;
139 setSeed(seeds ? *seeds : 19780503L, 0);
145 std::ofstream outFile( filename, std::ios::out ) ;
147 if (!outFile.bad()) {
149 std::vector<unsigned long> v =
put();
151 std::cout <<
"Result of v = put() is:\n";
153 for (
unsigned int i=0; i<v.size(); ++i) {
154 outFile << v[i] <<
"\n";
156 std::cout << v[i] <<
" ";
157 if (i%6==0) std::cout <<
"\n";
165 if (!outFile.bad()) {
166 outFile <<
theSeed << std::endl;
167 outFile << seq << std::endl;
179 std::ifstream inFile( filename, std::ios::in);
181 std::cout <<
" -- Engine state remains unchanged\n";
185 std::vector<unsigned long> v;
190 std::cout <<
"ivec = " << ivec <<
" xin = " << xin <<
" ";
191 if (ivec%3 == 0) std::cout <<
"\n";
194 inFile.clear(std::ios::badbit | inFile.rdstate());
195 std::cerr <<
"\nRandEngine state (vector) description improper."
196 <<
"\nrestoreStatus has failed."
197 <<
"\nInput stream is probably mispositioned now." << std::endl;
208 if (!inFile.bad() && !inFile.eof()) {
213 while (seq < count)
flat();
219 std::cout << std::endl;
220 std::cout <<
"---------- Rand engine status ----------" << std::endl;
221 std::cout <<
" Initial seed = " <<
theSeed << std::endl;
222 std::cout <<
" Shooted sequences = " << seq << std::endl;
223 std::cout <<
"----------------------------------------" << std::endl;
248 static bool prepared =
false;
249 static unsigned int iT;
250 static unsigned int iK;
251 static unsigned int iS;
256 if ( (RAND_MAX >> 31) > 0 )
260 iS = (
unsigned long)RAND_MAX + 1;
267 if( (
unsigned long) (RAND_MAX) <= (( (1uL) << 31 ) - 1 ) ) {
273 for ( m = 0; m < 64; ++m ) {
275 if (StoK == 0)
break;
282 for (
int i = 0; i < iK; ++i ) {
283 v = iS*v+rand(); ++seq;
286 return v & 0xFFFFFFFF;
290 else if ( (RAND_MAX >> 26) == 0 )
296 fS = (
unsigned long)RAND_MAX + 1;
297 double twoTo32 = std::ldexp(1.0,32);
299 for ( iK = 1; StoK < twoTo32; StoK *= fS, iK++ ) { }
302 for ( m = 0; m < 64; ++m ) {
304 if (StoK < 1.0)
break;
311 for (
int i = 0; i < iK; ++i ) {
312 v = fS*v+rand(); ++seq;
315 return ((
unsigned int)v) & 0xFFFFFFFF;
323 iS = (
unsigned long)RAND_MAX + 1;
325 for (iN = 0; SshiftN > 1; SshiftN >>= 1, iN++) { }
330 do { x1 = rand(); ++seq;}
while (x1 < (1<<16) );
331 do { x2 = rand(); ++seq;}
while (x2 < (1<<16) );
332 return x1 | (x2 << 16);
340 unsigned int x = rand() << 1; ++seq;
341 x ^= ( (x>>23) ^ (x>>7) ) ^1;
342 return x & 0xFFFFFFFF;
349 unsigned int x = rand() << 17; ++seq;
350 x ^= rand() << 2; ++seq;
351 x ^= ( (x>>23) ^ (x>>7) ) ^3;
352 return x & 0xFFFFFFFF;
361 return r/4294967296.0;
368 for (i=0; i<size; ++i)
376RandEngine::operator float() {
377 return float( flat() );
380RandEngine::operator
unsigned int() {
386 char beginMarker[] =
"RandEngine-begin";
387 char endMarker[] =
"RandEngine-end";
389 os <<
" " << beginMarker <<
"\n";
390 os <<
theSeed <<
" " << seq <<
" ";
391 os << endMarker <<
"\n";
396 std::vector<unsigned long> v;
397 v.push_back (engineIDulong<RandEngine>());
398 v.push_back(
static_cast<unsigned long>(
theSeed));
399 v.push_back(
static_cast<unsigned long>(seq));
409 char beginMarker [MarkerLen];
415 if (strcmp(beginMarker,
"RandEngine-begin")) {
416 is.clear(std::ios::badbit | is.rdstate());
417 std::cout <<
"\nInput stream mispositioned or"
418 <<
"\nRandEngine state description missing or"
419 <<
"\nwrong engine type found." << std::endl;
426 return "RandEngine-begin";
432 std::vector<unsigned long> v;
437 is.clear(std::ios::badbit | is.rdstate());
438 std::cerr <<
"\nRandEngine state (vector) description improper."
439 <<
"\ngetState() has failed."
440 <<
"\nInput stream is probably mispositioned now." << std::endl;
451 char endMarker [MarkerLen];
457 if (strcmp(endMarker,
"RandEngine-end")) {
458 is.clear(std::ios::badbit | is.rdstate());
459 std::cerr <<
"\nRandEngine state description incomplete."
460 <<
"\nInput stream is probably mispositioned now." << std::endl;
464 while (seq < count)
flat();
469 if ((v[0] & 0xffffffffUL) != engineIDulong<RandEngine>()) {
471 "\nRandEngine get:state vector has wrong ID word - state unchanged\n";
480 "\nRandEngine get:state vector has wrong length - state unchanged\n";
484 int count = (int)v[2];
486 while (seq < count)
flat();
static bool checkFile(std::istream &file, const std::string &filename, const std::string &classname, const std::string &methodname)
static void getTheTableSeeds(long *seeds, int index)
static std::string engineName()
virtual std::istream & get(std::istream &is)
void flatArray(const int size, double *vect)
virtual std::istream & getState(std::istream &is)
void saveStatus(const char filename[]="Rand.conf") const
void setSeed(long seed, int dum=0)
static std::string beginTag()
std::vector< unsigned long > put() const
void restoreStatus(const char filename[]="Rand.conf")
static const unsigned int VECTOR_STATE_SIZE
void setSeeds(const long *seeds, int dum=0)
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
static unsigned int thirtyTwoRandomBits(long &seq)
static unsigned int thirtyTwoRandomBits(long &seq)
static unsigned int thirtyTwoRandomBits(long &seq)