66 static const int MarkerLen = 64;
70 int Ranlux64Engine::numEngines = 0;
73 int Ranlux64Engine::maxIndex = 215;
78 template< std::size_t
n,
79 bool = n < std::size_t(std::numeric_limits<unsigned long>::digits) >
80 struct do_right_shift;
81 template< std::
size_t n >
82 struct do_right_shift<n,true>
84 unsigned long operator()(
unsigned long value) {
return value >>
n; }
86 template< std::
size_t n >
87 struct do_right_shift<
n,false>
92 template< std::
size_t nbits >
94 {
return do_right_shift<nbits>()(value); }
105 int cycle = std::abs(
int(numEngines/maxIndex));
106 int curIndex = std::abs(
int(numEngines%maxIndex));
108 long mask = ((cycle & 0x007fffff) << 8);
124 long seedlist[2]={seed,0};
126 advance ( 2*lux + 1 );
134 int cycle = std::abs(
int(rowIndex/maxIndex));
135 int row = std::abs(
int(rowIndex%maxIndex));
136 long mask = (( cycle & 0x000007ff ) << 20 );
158 if (index <= 0) update();
162 void Ranlux64Engine::update() {
191 if ( endIters == 1 ) {
192 y1 = randoms[ 4] - randoms[11] - carry;
199 randoms[11] = randoms[10];
200 randoms[10] = randoms[ 9];
201 randoms[ 9] = randoms[ 8];
202 randoms[ 8] = randoms[ 7];
203 randoms[ 7] = randoms[ 6];
204 randoms[ 6] = randoms[ 5];
205 randoms[ 5] = randoms[ 4];
206 randoms[ 4] = randoms[ 3];
207 randoms[ 3] = randoms[ 2];
208 randoms[ 2] = randoms[ 1];
209 randoms[ 1] = randoms[ 0];
215 for ( m = 0, nr = 11, ns = 4; m < endIters; ++m, --nr ) {
216 y1 = randoms [ns] - randoms[nr] - carry;
231 for (m=0; m<12; m++) {
236 for (m=11; m>=0; --m) {
237 randoms[m] = temp[ns];
252 void Ranlux64Engine::advance(
int dozens) {
254 register double y1,
y2, y3;
256 register double zero = 0.0;
257 register double one = 1.0;
278 for ( k = dozens; k > 0; --k ) {
280 y1 = randoms[ 4] - randoms[11] - carry;
282 y2 = randoms[ 3] - randoms[10];
289 y3 = randoms[ 2] - randoms[ 9];
296 y1 = randoms[ 1] - randoms[ 8];
303 y2 = randoms[ 0] - randoms[ 7];
310 y3 = randoms[11] - randoms[ 6];
317 y1 = randoms[10] - randoms[ 5];
324 y2 = randoms[ 9] - randoms[ 4];
331 y3 = randoms[ 8] - randoms[ 3];
338 y1 = randoms[ 7] - randoms[ 2];
345 y2 = randoms[ 6] - randoms[ 1];
352 y3 = randoms[ 5] - randoms[ 0];
370 for(
int i=0; i < size; ++i ) {
383 const int ecuyer_a(53668);
384 const int ecuyer_b(40014);
385 const int ecuyer_c(12211);
386 const int ecuyer_d(2147483563);
388 const int lux_levels[3] = {109, 202, 397};
391 if( (lux > 2)||(lux < 0) ){
392 pDiscard = (lux >= 12) ? (lux-12) : lux_levels[1];
394 pDiscard = lux_levels[luxury];
396 pDozens = pDiscard / 12;
397 endIters = pDiscard % 12;
400 long next_seed = seed;
403 next_seed &= 0xffffffff;
404 while( next_seed >= ecuyer_d ) {
405 next_seed -= ecuyer_d;
408 for(i = 0;i != 24;i++){
409 k_multiple = next_seed / ecuyer_a;
410 next_seed = ecuyer_b * (next_seed - k_multiple * ecuyer_a)
411 - k_multiple * ecuyer_c;
413 next_seed += ecuyer_d;
415 next_seed &= 0xffffffff;
416 init_table[i] = next_seed;
419 if(
sizeof(
long) >= 8 ) {
420 long topbits1, topbits2;
422 topbits1 = ( seed >> 32) & 0xffff ;
423 topbits2 = ( seed >> 48) & 0xffff ;
425 topbits1 = detail::rshift<32>(seed) & 0xffff ;
426 topbits2 = detail::rshift<48>(seed) & 0xffff ;
428 init_table[0] ^= topbits1;
429 init_table[2] ^= topbits2;
434 for(i = 0;i < 12; i++){
458 const int ecuyer_a = 53668;
459 const int ecuyer_b = 40014;
460 const int ecuyer_c = 12211;
461 const int ecuyer_d = 2147483563;
463 const int lux_levels[3] = {109, 202, 397};
480 if( (lux > 2)||(lux < 0) ){
481 pDiscard = (lux >= 12) ? (lux-12) : lux_levels[1];
483 pDiscard = lux_levels[luxury];
485 pDozens = pDiscard / 12;
486 endIters = pDiscard % 12;
489 long next_seed = *seeds;
493 for( i = 0;(i != 24)&&(*seedptr != 0);i++){
494 init_table[i] = *seedptr & 0xffffffff;
499 next_seed = init_table[i-1];
501 k_multiple = next_seed / ecuyer_a;
502 next_seed = ecuyer_b * (next_seed - k_multiple * ecuyer_a)
503 - k_multiple * ecuyer_c;
505 next_seed += ecuyer_d;
507 next_seed &= 0xffffffff;
508 init_table[i] = next_seed;
512 for(i = 0;i < 12; i++){
525 std::ofstream
outFile( filename, std::ios::out ) ;
526 if (!outFile.bad()) {
528 std::vector<unsigned long>
v =
put();
529 for (
unsigned int i=0; i<v.size(); ++i) {
530 outFile << v[i] <<
"\n";
539 std::cerr <<
" -- Engine state remains unchanged\n";
543 std::vector<unsigned long>
v;
548 inFile.clear(std::ios::badbit | inFile.rdstate());
549 std::cerr <<
"\nJamesRandom state (vector) description improper."
550 <<
"\nrestoreStatus has failed."
551 <<
"\nInput stream is probably mispositioned now." << std::endl;
560 if (!inFile.bad() && !inFile.eof()) {
562 for (
int i=0; i<12; ++i) {
563 inFile >> randoms[i];
565 inFile >> carry; inFile >> index;
566 inFile >> luxury; inFile >> pDiscard;
567 pDozens = pDiscard / 12;
568 endIters = pDiscard % 12;
574 std::cout << std::endl;
575 std::cout <<
"--------- Ranlux engine status ---------" << std::endl;
576 std::cout <<
" Initial seed = " <<
theSeed << std::endl;
577 std::cout <<
" randoms[] = ";
578 for (
int i=0; i<12; ++i) {
579 std::cout << randoms[i] << std::endl;
581 std::cout << std::endl;
582 std::cout <<
" carry = " << carry <<
", index = " << index << std::endl;
583 std::cout <<
" luxury = " << luxury <<
" pDiscard = "
584 << pDiscard << std::endl;
585 std::cout <<
"----------------------------------------" << std::endl;
590 char beginMarker[] =
"Ranlux64Engine-begin";
591 os << beginMarker <<
"\nUvec\n";
592 std::vector<unsigned long>
v =
put();
593 for (
unsigned int i=0; i<v.size(); ++i) {
600 std::vector<unsigned long>
v;
601 v.push_back (engineIDulong<Ranlux64Engine>());
602 std::vector<unsigned long> t;
603 for (
int i=0; i<12; ++i) {
605 v.push_back(t[0]); v.push_back(t[1]);
608 v.push_back(t[0]); v.push_back(t[1]);
609 v.push_back(static_cast<unsigned long>(index));
610 v.push_back(static_cast<unsigned long>(luxury));
611 v.push_back(static_cast<unsigned long>(pDiscard));
617 char beginMarker [MarkerLen];
623 if (strcmp(beginMarker,
"Ranlux64Engine-begin")) {
624 is.clear(std::ios::badbit | is.rdstate());
625 std::cerr <<
"\nInput stream mispositioned or"
626 <<
"\nRanlux64Engine state description missing or"
627 <<
"\nwrong engine type found." << std::endl;
634 return "Ranlux64Engine-begin";
640 std::vector<unsigned long>
v;
645 is.clear(std::ios::badbit | is.rdstate());
646 std::cerr <<
"\nRanlux64Engine state (vector) description improper."
647 <<
"\ngetState() has failed."
648 <<
"\nInput stream is probably mispositioned now." << std::endl;
659 char endMarker [MarkerLen];
660 for (
int i=0; i<12; ++i) {
663 is >> carry; is >> index;
664 is >> luxury; is >> pDiscard;
665 pDozens = pDiscard / 12;
666 endIters = pDiscard % 12;
670 if (strcmp(endMarker,
"Ranlux64Engine-end")) {
671 is.clear(std::ios::badbit | is.rdstate());
672 std::cerr <<
"\nRanlux64Engine state description incomplete."
673 <<
"\nInput stream is probably mispositioned now." << std::endl;
680 if ((v[0] & 0xffffffffUL) != engineIDulong<Ranlux64Engine>()) {
682 "\nRanlux64Engine get:state vector has wrong ID word - state unchanged\n";
691 "\nRanlux64Engine get:state vector has wrong length - state unchanged\n";
694 std::vector<unsigned long> t(2);
695 for (
int i=0; i<12; ++i) {
696 t[0] = v[2*i+1]; t[1] = v[2*i+2];
699 t[0] = v[25]; t[1] = v[26];
unsigned long rshift(unsigned long value)
virtual ~Ranlux64Engine()
static double twoToMinus_48()
static double twoToMinus_32()
static bool checkFile(std::istream &file, const std::string &filename, const std::string &classname, const std::string &methodname)
static std::string engineName()
std::vector< unsigned long > put() const
void setSeeds(const long *seeds, int lux=1)
static const unsigned int VECTOR_STATE_SIZE
void setSeed(long seed, int lux=1)
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
void restoreStatus(const char filename[]="Ranlux64.conf")
static std::vector< unsigned long > dto2longs(double d)
static std::string beginTag()
virtual std::istream & getState(std::istream &is)
unsigned long operator()(unsigned long)
const XML_Char int const XML_Char * value
void flatArray(const int size, double *vect)
static double twoToMinus_49()
void saveStatus(const char filename[]="Ranlux64.conf") const
virtual std::istream & get(std::istream &is)
static void getTheTableSeeds(long *seeds, int index)
static double longs2double(const std::vector< unsigned long > &v)