40#include "CLHEP/Random/defs.h"
41#include "CLHEP/Random/Random.h"
42#include "CLHEP/Random/RanluxEngine.h"
43#include "CLHEP/Random/engineIDulong.h"
44#include "CLHEP/Utility/atomic_int.h"
54 #include "CLHEP/Random/DoubConv.h"
55 bool flat_trace =
false;
65 const int maxIndex = 215;
68static const int MarkerLen = 64;
75 long seedlist[2]={0,0};
90 long seedlist[2]={0,0};
93 int numEngines = numberOfEngines++;
94 int cycle = std::abs(
int(numEngines/maxIndex));
95 int curIndex = std::abs(
int(numEngines%maxIndex));
97 long mask = ((cycle & 0x007fffff) << 8);
99 seed = seedlist[0]^mask;
112 long seedlist[2]={0,0};
115 int cycle = std::abs(
int(rowIndex/maxIndex));
116 int row = std::abs(
int(rowIndex%maxIndex));
117 int col = std::abs(
int(colIndex%2));
118 long mask = (( cycle & 0x000007ff ) << 20 );
120 seed = ( seedlist[col] )^mask;
145 const int ecuyer_a = 53668;
146 const int ecuyer_b = 40014;
147 const int ecuyer_c = 12211;
148 const int ecuyer_d = 2147483563;
150 const int lux_levels[5] = {0,24,73,199,365};
152 long int_seed_table[24];
153 long next_seed = seed;
161 if( (lux > 4)||(lux < 0) ){
165 nskip = lux_levels[3];
169 nskip = lux_levels[luxury];
173 for(i = 0;i != 24;i++){
174 k_multiple = next_seed / ecuyer_a;
175 next_seed = ecuyer_b * (next_seed - k_multiple * ecuyer_a)
176 - k_multiple * ecuyer_c ;
177 if(next_seed < 0)next_seed += ecuyer_d;
178 int_seed_table[i] = next_seed % int_modulus;
181 for(i = 0;i != 24;i++)
195 const int ecuyer_a = 53668;
196 const int ecuyer_b = 40014;
197 const int ecuyer_c = 12211;
198 const int ecuyer_d = 2147483563;
200 const int lux_levels[5] = {0,24,73,199,365};
202 long int_seed_table[24];
203 long k_multiple,next_seed;
220 if( (lux > 4)||(lux < 0) ){
224 nskip = lux_levels[3];
228 nskip = lux_levels[luxury];
231 for( i = 0;(i != 24)&&(*seedptr != 0);i++){
232 int_seed_table[i] = *seedptr % int_modulus;
237 next_seed = int_seed_table[i-1];
239 k_multiple = next_seed / ecuyer_a;
240 next_seed = ecuyer_b * (next_seed - k_multiple * ecuyer_a)
241 - k_multiple * ecuyer_c ;
242 if(next_seed < 0)next_seed += ecuyer_d;
243 int_seed_table[i] = next_seed % int_modulus;
247 for(i = 0;i != 24;i++)
261 std::ofstream outFile( filename, std::ios::out ) ;
262 if (!outFile.bad()) {
264 std::vector<unsigned long> v =
put();
266 std::cout <<
"Result of v = put() is:\n";
268 for (
unsigned int i=0; i<v.size(); ++i) {
269 outFile << v[i] <<
"\n";
271 std::cout << v[i] <<
" ";
272 if (i%6==0) std::cout <<
"\n";
280 if (!outFile.bad()) {
281 outFile <<
theSeed << std::endl;
282 for (
int i=0; i<24; ++i)
283 outFile <<std::setprecision(20) << float_seed_table[i] <<
" ";
284 outFile << std::endl;
285 outFile << i_lag <<
" " << j_lag << std::endl;
286 outFile << std::setprecision(20) << carry <<
" " << count24 << std::endl;
287 outFile << luxury <<
" " << nskip << std::endl;
294 std::ifstream inFile( filename, std::ios::in);
296 std::cerr <<
" -- Engine state remains unchanged\n";
300 std::vector<unsigned long> v;
305 std::cout <<
"ivec = " << ivec <<
" xin = " << xin <<
" ";
306 if (ivec%3 == 0) std::cout <<
"\n";
309 inFile.clear(std::ios::badbit | inFile.rdstate());
310 std::cerr <<
"\nRanluxEngine state (vector) description improper."
311 <<
"\nrestoreStatus has failed."
312 <<
"\nInput stream is probably mispositioned now." << std::endl;
321 if (!inFile.bad() && !inFile.eof()) {
323 for (
int i=0; i<24; ++i)
324 inFile >> float_seed_table[i];
325 inFile >> i_lag; inFile >> j_lag;
326 inFile >> carry; inFile >> count24;
327 inFile >> luxury; inFile >> nskip;
333 std::cout << std::endl;
334 std::cout <<
"--------- Ranlux engine status ---------" << std::endl;
335 std::cout <<
" Initial seed = " <<
theSeed << std::endl;
336 std::cout <<
" float_seed_table[] = ";
337 for (
int i=0; i<24; ++i)
338 std::cout << float_seed_table[i] <<
" ";
339 std::cout << std::endl;
340 std::cout <<
" i_lag = " << i_lag <<
", j_lag = " << j_lag << std::endl;
341 std::cout <<
" carry = " << carry <<
", count24 = " << count24 << std::endl;
342 std::cout <<
" luxury = " << luxury <<
" nskip = " << nskip << std::endl;
343 std::cout <<
"----------------------------------------" << std::endl;
352 uni = float_seed_table[j_lag] - float_seed_table[i_lag] - carry;
355 std::cout <<
"float_seed_table[" << j_lag <<
"] = "
356 << float_seed_table[j_lag]
357 <<
" float_seed_table[" << i_lag <<
"] = " << float_seed_table[i_lag]
358 <<
" uni = " << uni <<
"\n";
359 std::cout << float_seed_table[j_lag]
360 <<
" - " << float_seed_table[i_lag]
361 <<
" - " << carry <<
" = "
362 << (
double)float_seed_table[j_lag]
363 - (
double) float_seed_table[i_lag] - (
double)carry
374 float_seed_table[i_lag] = uni;
377 if(i_lag < 0) i_lag = 23;
378 if(j_lag < 0) j_lag = 23;
394 std::cout <<
"carry = " << carry <<
"\n";
397 for( i = 0; i != nskip ; i++){
398 uni = float_seed_table[j_lag] - float_seed_table[i_lag] - carry;
405 float_seed_table[i_lag] = uni;
408 double xfst = float_seed_table[i_lag];
409 std::cout <<
"fst[" << i_lag <<
"] = "
415 if(i_lag < 0)i_lag = 23;
416 if(j_lag < 0) j_lag = 23;
421 std::cout <<
"next_random = " << next_random <<
"\n";
425 return (
double) next_random;
435 for (index=0; index<size; ++index) {
436 uni = float_seed_table[j_lag] - float_seed_table[i_lag] - carry;
444 float_seed_table[i_lag] = uni;
447 if(i_lag < 0) i_lag = 23;
448 if(j_lag < 0) j_lag = 23;
455 vect[index] = (
double)next_random;
463 for( i = 0; i != nskip ; i++){
464 uni = float_seed_table[j_lag] - float_seed_table[i_lag] - carry;
471 float_seed_table[i_lag] = uni;
474 if(i_lag < 0)i_lag = 23;
475 if(j_lag < 0) j_lag = 23;
485RanluxEngine::operator float() {
486 return float( flat() );
489RanluxEngine::operator
unsigned int() {
490 return ((
unsigned int)(flat() * exponent_bit_32()) & 0xffffffff) |
491 (((
unsigned int)(float_seed_table[i_lag]*exponent_bit_32())>>16) & 0xff);
498 char beginMarker[] =
"RanluxEngine-begin";
499 os << beginMarker <<
"\nUvec\n";
500 std::vector<unsigned long> v =
put();
501 for (
unsigned int i=0; i<v.size(); ++i) {
506 char endMarker[] =
"RanluxEngine-end";
507 long pr = os.precision(20);
508 os <<
" " << beginMarker <<
" ";
510 for (
int i=0; i<24; ++i) {
511 os << float_seed_table[i] <<
"\n";
513 os << i_lag <<
" " << j_lag <<
"\n";
514 os << carry <<
" " << count24 <<
" ";
515 os << luxury <<
" " << nskip <<
"\n";
516 os << endMarker <<
"\n";
523 std::vector<unsigned long> v;
524 v.push_back (engineIDulong<RanluxEngine>());
526 std::cout <<
"RanluxEngine put: ID is " << v[0] <<
"\n";
528 for (
int i=0; i<24; ++i) {
532 std::cout <<
"v[" << i+1 <<
"] = " << v[i+1] <<
533 " float_seed_table[" << i <<
"] = " << float_seed_table[i] <<
"\n";
536 v.push_back(
static_cast<unsigned long>(i_lag));
537 v.push_back(
static_cast<unsigned long>(j_lag));
539 v.push_back(
static_cast<unsigned long>(count24));
540 v.push_back(
static_cast<unsigned long>(luxury));
541 v.push_back(
static_cast<unsigned long>(nskip));
543 std::cout <<
"i_lag: " << v[25] <<
" j_lag: " << v[26]
544 <<
" carry: " << v[27] <<
"\n";
545 std::cout <<
"count24: " << v[28] <<
" luxury: " << v[29]
546 <<
" nskip: " << v[30] <<
"\n";
556 char beginMarker [MarkerLen];
562 if (strcmp(beginMarker,
"RanluxEngine-begin")) {
563 is.clear(std::ios::badbit | is.rdstate());
564 std::cerr <<
"\nInput stream mispositioned or"
565 <<
"\nRanluxEngine state description missing or"
566 <<
"\nwrong engine type found." << std::endl;
573 return "RanluxEngine-begin";
579 std::vector<unsigned long> v;
584 is.clear(std::ios::badbit | is.rdstate());
585 std::cerr <<
"\nRanluxEngine state (vector) description improper."
586 <<
"\ngetState() has failed."
587 <<
"\nInput stream is probably mispositioned now." << std::endl;
592 std::cout <<
"RanluxEngine::getState -- v[" << v.size()-1
593 <<
"] = " << v[v.size()-1] <<
"\n";
602 char endMarker [MarkerLen];
603 for (
int i=0; i<24; ++i) {
604 is >> float_seed_table[i];
606 is >> i_lag; is >> j_lag;
607 is >> carry; is >> count24;
608 is >> luxury; is >> nskip;
612 if (strcmp(endMarker,
"RanluxEngine-end")) {
613 is.clear(std::ios::badbit | is.rdstate());
614 std::cerr <<
"\nRanluxEngine state description incomplete."
615 <<
"\nInput stream is probably mispositioned now." << std::endl;
622 if ((v[0] & 0xffffffffUL) != engineIDulong<RanluxEngine>()) {
624 "\nRanluxEngine get:state vector has wrong ID word - state unchanged\n";
633 "\nRanluxEngine get:state vector has wrong length - state unchanged\n";
636 for (
int i=0; i<24; ++i) {
640 "float_seed_table[" << i <<
"] = " << float_seed_table[i] <<
"\n";
646 count24 = (int)v[28];
650 std::cout <<
"i_lag: " << i_lag <<
" j_lag: " << j_lag
651 <<
" carry: " << carry <<
"\n";
652 std::cout <<
"count24: " << count24 <<
" luxury: " << luxury
653 <<
" nskip: " << nskip <<
"\n";
#define CLHEP_ATOMIC_INT_TYPE
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)
static const unsigned int VECTOR_STATE_SIZE
void flatArray(const int size, double *vect)
void setSeeds(const long *seeds, int lxr=3)
virtual std::istream & getState(std::istream &is)
void saveStatus(const char filename[]="Ranlux.conf") const
std::vector< unsigned long > put() const
static std::string beginTag()
virtual std::istream & get(std::istream &is)
void restoreStatus(const char filename[]="Ranlux.conf")
static std::string engineName()
void setSeed(long seed, int lxr=3)
bool possibleKeywordInput(IS &is, const std::string &key, T &t)