40 #include "CLHEP/Random/defs.h"
41 #include "CLHEP/Random/Random.h"
42 #include "CLHEP/Random/RanluxEngine.h"
43 #include "CLHEP/Random/engineIDulong.h"
48 #include "CLHEP/Random/DoubConv.hh"
49 bool flat_trace =
false;
55 static const int MarkerLen = 64;
60 int RanluxEngine::numEngines = 0;
63 int RanluxEngine::maxIndex = 215;
68 long seedlist[2]={0,0};
83 long seedlist[2]={0,0};
86 int cycle = std::abs(
int(numEngines/maxIndex));
87 int curIndex = std::abs(
int(numEngines%maxIndex));
89 long mask = ((cycle & 0x007fffff) << 8);
91 seed = seedlist[0]^mask;
104 long seedlist[2]={0,0};
107 int cycle = std::abs(
int(rowIndex/maxIndex));
108 int row = std::abs(
int(rowIndex%maxIndex));
109 int col = std::abs(
int(colIndex%2));
110 long mask = (( cycle & 0x000007ff ) << 20 );
112 seed = ( seedlist[col] )^mask;
137 const int ecuyer_a = 53668;
138 const int ecuyer_b = 40014;
139 const int ecuyer_c = 12211;
140 const int ecuyer_d = 2147483563;
142 const int lux_levels[5] = {0,24,73,199,365};
144 long int_seed_table[24];
145 long next_seed = seed;
153 if( (lux > 4)||(lux < 0) ){
157 nskip = lux_levels[3];
161 nskip = lux_levels[luxury];
165 for(i = 0;i != 24;i++){
166 k_multiple = next_seed / ecuyer_a;
167 next_seed = ecuyer_b * (next_seed - k_multiple * ecuyer_a)
168 - k_multiple * ecuyer_c ;
169 if(next_seed < 0)next_seed += ecuyer_d;
170 int_seed_table[i] = next_seed % int_modulus;
173 for(i = 0;i != 24;i++)
187 const int ecuyer_a = 53668;
188 const int ecuyer_b = 40014;
189 const int ecuyer_c = 12211;
190 const int ecuyer_d = 2147483563;
192 const int lux_levels[5] = {0,24,73,199,365};
194 long int_seed_table[24];
195 long k_multiple,next_seed;
212 if( (lux > 4)||(lux < 0) ){
216 nskip = lux_levels[3];
220 nskip = lux_levels[luxury];
223 for( i = 0;(i != 24)&&(*seedptr != 0);i++){
224 int_seed_table[i] = *seedptr % int_modulus;
229 next_seed = int_seed_table[i-1];
231 k_multiple = next_seed / ecuyer_a;
232 next_seed = ecuyer_b * (next_seed - k_multiple * ecuyer_a)
233 - k_multiple * ecuyer_c ;
234 if(next_seed < 0)next_seed += ecuyer_d;
235 int_seed_table[i] = next_seed % int_modulus;
239 for(i = 0;i != 24;i++)
253 std::ofstream outFile( filename, std::ios::out ) ;
254 if (!outFile.bad()) {
256 std::vector<unsigned long> v =
put();
258 std::cout <<
"Result of v = put() is:\n";
260 for (
unsigned int i=0; i<v.size(); ++i) {
261 outFile << v[i] <<
"\n";
263 std::cout << v[i] <<
" ";
264 if (i%6==0) std::cout <<
"\n";
272 if (!outFile.bad()) {
273 outFile <<
theSeed << std::endl;
274 for (
int i=0; i<24; ++i)
275 outFile <<std::setprecision(20) << float_seed_table[i] <<
" ";
276 outFile << std::endl;
277 outFile << i_lag <<
" " << j_lag << std::endl;
278 outFile << std::setprecision(20) << carry <<
" " << count24 << std::endl;
279 outFile << luxury <<
" " << nskip << std::endl;
286 std::ifstream inFile( filename, std::ios::in);
288 std::cerr <<
" -- Engine state remains unchanged\n";
292 std::vector<unsigned long> v;
297 std::cout <<
"ivec = " << ivec <<
" xin = " << xin <<
" ";
298 if (ivec%3 == 0) std::cout <<
"\n";
301 inFile.clear(std::ios::badbit | inFile.rdstate());
302 std::cerr <<
"\nRanluxEngine state (vector) description improper."
303 <<
"\nrestoreStatus has failed."
304 <<
"\nInput stream is probably mispositioned now." << std::endl;
313 if (!inFile.bad() && !inFile.eof()) {
315 for (
int i=0; i<24; ++i)
316 inFile >> float_seed_table[i];
317 inFile >> i_lag; inFile >> j_lag;
318 inFile >> carry; inFile >> count24;
319 inFile >> luxury; inFile >> nskip;
325 std::cout << std::endl;
326 std::cout <<
"--------- Ranlux engine status ---------" << std::endl;
327 std::cout <<
" Initial seed = " <<
theSeed << std::endl;
328 std::cout <<
" float_seed_table[] = ";
329 for (
int i=0; i<24; ++i)
330 std::cout << float_seed_table[i] <<
" ";
331 std::cout << std::endl;
332 std::cout <<
" i_lag = " << i_lag <<
", j_lag = " << j_lag << std::endl;
333 std::cout <<
" carry = " << carry <<
", count24 = " << count24 << std::endl;
334 std::cout <<
" luxury = " << luxury <<
" nskip = " << nskip << std::endl;
335 std::cout <<
"----------------------------------------" << std::endl;
344 uni = float_seed_table[j_lag] - float_seed_table[i_lag] - carry;
347 std::cout <<
"float_seed_table[" << j_lag <<
"] = "
348 << float_seed_table[j_lag]
349 <<
" float_seed_table[" << i_lag <<
"] = " << float_seed_table[i_lag]
350 <<
" uni = " << uni <<
"\n";
351 std::cout << float_seed_table[j_lag]
352 <<
" - " << float_seed_table[i_lag]
353 <<
" - " << carry <<
" = "
354 << (
double)float_seed_table[j_lag]
355 - (
double) float_seed_table[i_lag] - (
double)carry
366 float_seed_table[i_lag] = uni;
369 if(i_lag < 0) i_lag = 23;
370 if(j_lag < 0) j_lag = 23;
386 std::cout <<
"carry = " << carry <<
"\n";
389 for( i = 0; i != nskip ; i++){
390 uni = float_seed_table[j_lag] - float_seed_table[i_lag] - carry;
397 float_seed_table[i_lag] = uni;
400 double xfst = float_seed_table[i_lag];
401 std::cout <<
"fst[" << i_lag <<
"] = "
407 if(i_lag < 0)i_lag = 23;
408 if(j_lag < 0) j_lag = 23;
413 std::cout <<
"next_random = " << next_random <<
"\n";
417 return (
double) next_random;
427 for (index=0; index<size; ++index) {
428 uni = float_seed_table[j_lag] - float_seed_table[i_lag] - carry;
436 float_seed_table[i_lag] = uni;
439 if(i_lag < 0) i_lag = 23;
440 if(j_lag < 0) j_lag = 23;
447 vect[index] = (
double)next_random;
455 for( i = 0; i != nskip ; i++){
456 uni = float_seed_table[j_lag] - float_seed_table[i_lag] - carry;
463 float_seed_table[i_lag] = uni;
466 if(i_lag < 0)i_lag = 23;
467 if(j_lag < 0) j_lag = 23;
473 RanluxEngine::operator
unsigned int() {
474 return ((
unsigned int)(flat() * exponent_bit_32()) & 0xffffffff) |
475 (((
unsigned int)(float_seed_table[i_lag]*exponent_bit_32())>>16) & 0xff);
482 char beginMarker[] =
"RanluxEngine-begin";
483 os << beginMarker <<
"\nUvec\n";
484 std::vector<unsigned long> v =
put();
485 for (
unsigned int i=0; i<v.size(); ++i) {
490 char endMarker[] =
"RanluxEngine-end";
491 int pr = os.precision(20);
492 os <<
" " << beginMarker <<
" ";
494 for (
int i=0; i<24; ++i) {
495 os << float_seed_table[i] <<
"\n";
497 os << i_lag <<
" " << j_lag <<
"\n";
498 os << carry <<
" " << count24 <<
" ";
499 os << luxury <<
" " << nskip <<
"\n";
500 os << endMarker <<
"\n";
507 std::vector<unsigned long> v;
508 v.push_back (engineIDulong<RanluxEngine>());
510 std::cout <<
"RanluxEngine put: ID is " << v[0] <<
"\n";
512 for (
int i=0; i<24; ++i) {
516 std::cout <<
"v[" << i+1 <<
"] = " << v[i+1] <<
517 " float_seed_table[" << i <<
"] = " << float_seed_table[i] <<
"\n";
520 v.push_back(
static_cast<unsigned long>(i_lag));
521 v.push_back(
static_cast<unsigned long>(j_lag));
523 v.push_back(
static_cast<unsigned long>(count24));
524 v.push_back(
static_cast<unsigned long>(luxury));
525 v.push_back(
static_cast<unsigned long>(nskip));
527 std::cout <<
"i_lag: " << v[25] <<
" j_lag: " << v[26]
528 <<
" carry: " << v[27] <<
"\n";
529 std::cout <<
"count24: " << v[28] <<
" luxury: " << v[29]
530 <<
" nskip: " << v[30] <<
"\n";
540 char beginMarker [MarkerLen];
546 if (strcmp(beginMarker,
"RanluxEngine-begin")) {
547 is.clear(std::ios::badbit | is.rdstate());
548 std::cerr <<
"\nInput stream mispositioned or"
549 <<
"\nRanluxEngine state description missing or"
550 <<
"\nwrong engine type found." << std::endl;
557 return "RanluxEngine-begin";
563 std::vector<unsigned long> v;
568 is.clear(std::ios::badbit | is.rdstate());
569 std::cerr <<
"\nRanluxEngine state (vector) description improper."
570 <<
"\ngetState() has failed."
571 <<
"\nInput stream is probably mispositioned now." << std::endl;
576 std::cout <<
"RanluxEngine::getState -- v[" << v.size()-1
577 <<
"] = " << v[v.size()-1] <<
"\n";
586 char endMarker [MarkerLen];
587 for (
int i=0; i<24; ++i) {
588 is >> float_seed_table[i];
590 is >> i_lag; is >> j_lag;
591 is >> carry; is >> count24;
592 is >> luxury; is >> nskip;
596 if (strcmp(endMarker,
"RanluxEngine-end")) {
597 is.clear(std::ios::badbit | is.rdstate());
598 std::cerr <<
"\nRanluxEngine state description incomplete."
599 <<
"\nInput stream is probably mispositioned now." << std::endl;
606 if ((v[0] & 0xffffffffUL) != engineIDulong<RanluxEngine>()) {
608 "\nRanluxEngine get:state vector has wrong ID word - state unchanged\n";
617 "\nRanluxEngine get:state vector has wrong length - state unchanged\n";
620 for (
int i=0; i<24; ++i) {
624 "float_seed_table[" << i <<
"] = " << float_seed_table[i] <<
"\n";
634 std::cout <<
"i_lag: " << i_lag <<
" j_lag: " << j_lag
635 <<
" carry: " << carry <<
"\n";
636 std::cout <<
"count24: " << count24 <<
" luxury: " << luxury
637 <<
" nskip: " << nskip <<
"\n";
static std::string d2x(double d)
static double mantissa_bit_12()
static bool checkFile(std::istream &file, const std::string &filename, const std::string &classname, const std::string &methodname)
static double mantissa_bit_24()
static void getTheTableSeeds(long *seeds, int index)
void flatArray(const int size, double *vect)
virtual std::istream & getState(std::istream &is)
void saveStatus(const char filename[]="Ranlux.conf") const
std::vector< unsigned long > put() const
void setSeeds(const long *seeds, int lux=3)
static std::string beginTag()
static const unsigned int VECTOR_STATE_SIZE
virtual std::istream & get(std::istream &is)
void setSeed(long seed, int lux=3)
void restoreStatus(const char filename[]="Ranlux.conf")
static std::string engineName()
bool possibleKeywordInput(IS &is, const std::string &key, T &t)