CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
CLHEP::Ranlux64Engine Class Reference

#include <Ranlux64Engine.h>

+ Inheritance diagram for CLHEP::Ranlux64Engine:

Public Member Functions

 Ranlux64Engine (std::istream &is)
 
 Ranlux64Engine ()
 
 Ranlux64Engine (long seed, int lxr=1)
 
 Ranlux64Engine (int rowIndex, int colIndex, int lxr)
 
virtual ~Ranlux64Engine ()
 
double flat ()
 
void flatArray (const int size, double *vect)
 
void setSeed (long seed, int lxr=1)
 
void setSeeds (const long *seeds, int lxr=1)
 
void saveStatus (const char filename[]="Ranlux64.conf") const
 
void restoreStatus (const char filename[]="Ranlux64.conf")
 
void showStatus () const
 
int getLuxury () const
 
virtual std::ostream & put (std::ostream &os) const
 
virtual std::istream & get (std::istream &is)
 
virtual std::istream & getState (std::istream &is)
 
std::string name () const
 
std::vector< unsigned long > put () const
 
bool get (const std::vector< unsigned long > &v)
 
bool getState (const std::vector< unsigned long > &v)
 
- Public Member Functions inherited from CLHEP::HepRandomEngine
 HepRandomEngine ()
 
virtual ~HepRandomEngine ()
 
bool operator== (const HepRandomEngine &engine)
 
bool operator!= (const HepRandomEngine &engine)
 
virtual double flat ()=0
 
virtual void flatArray (const int size, double *vect)=0
 
virtual void setSeed (long seed, int)=0
 
virtual void setSeeds (const long *seeds, int)=0
 
virtual void saveStatus (const char filename[]="Config.conf") const =0
 
virtual void restoreStatus (const char filename[]="Config.conf")=0
 
virtual void showStatus () const =0
 
virtual std::string name () const =0
 
virtual std::ostream & put (std::ostream &os) const
 
virtual std::istream & get (std::istream &is)
 
virtual std::istream & getState (std::istream &is)
 
virtual std::vector< unsigned long > put () const
 
virtual bool get (const std::vector< unsigned long > &v)
 
virtual bool getState (const std::vector< unsigned long > &v)
 
long getSeed () const
 
const long * getSeeds () const
 
virtual operator double ()
 
virtual operator float ()
 
virtual operator unsigned int ()
 

Static Public Member Functions

static std::string beginTag ()
 
static std::string engineName ()
 
- Static Public Member Functions inherited from CLHEP::HepRandomEngine
static std::string beginTag ()
 
static HepRandomEnginenewEngine (std::istream &is)
 
static HepRandomEnginenewEngine (const std::vector< unsigned long > &v)
 

Static Public Attributes

static const unsigned int VECTOR_STATE_SIZE = 30
 

Additional Inherited Members

- Static Protected Member Functions inherited from CLHEP::HepRandomEngine
static double exponent_bit_32 ()
 
static double mantissa_bit_12 ()
 
static double mantissa_bit_24 ()
 
static double mantissa_bit_32 ()
 
static double twoToMinus_32 ()
 
static double twoToMinus_48 ()
 
static double twoToMinus_49 ()
 
static double twoToMinus_53 ()
 
static double nearlyTwoToMinus_54 ()
 
static bool checkFile (std::istream &file, const std::string &filename, const std::string &classname, const std::string &methodname)
 
- Protected Attributes inherited from CLHEP::HepRandomEngine
long theSeed
 
const long * theSeeds
 

Detailed Description

Author

Definition at line 50 of file Ranlux64Engine.h.

Constructor & Destructor Documentation

◆ Ranlux64Engine() [1/4]

CLHEP::Ranlux64Engine::Ranlux64Engine ( std::istream &  is)

Definition at line 153 of file Ranlux64Engine.cc.

155{
156 is >> *this;
157}

◆ Ranlux64Engine() [2/4]

CLHEP::Ranlux64Engine::Ranlux64Engine ( )

Definition at line 109 of file Ranlux64Engine.cc.

111{
112 luxury = 1;
113 int numEngines = numberOfEngines++;
114 int cycle = std::abs(int(numEngines/maxIndex));
115 int curIndex = std::abs(int(numEngines%maxIndex));
116
117 long mask = ((cycle & 0x007fffff) << 8);
118 long seedlist[2];
119 HepRandom::getTheTableSeeds( seedlist, curIndex );
120 seedlist[0] ^= mask;
121 seedlist[1] = 0;
122
123 setSeeds(seedlist, luxury);
124 advance ( 8 ); // Discard some iterations and ensure that
125 // this sequence won't match one where seeds
126 // were provided.
127}
static void getTheTableSeeds(long *seeds, int index)
Definition: Random.cc:256
void setSeeds(const long *seeds, int lxr=1)

◆ Ranlux64Engine() [3/4]

CLHEP::Ranlux64Engine::Ranlux64Engine ( long  seed,
int  lxr = 1 
)

Definition at line 129 of file Ranlux64Engine.cc.

131{
132 luxury = lux;
133 long seedlist[2]={seed,0};
134 setSeeds(seedlist, lux);
135 advance ( 2*lux + 1 ); // Discard some iterations to use a different
136 // point in the sequence.
137}

◆ Ranlux64Engine() [4/4]

CLHEP::Ranlux64Engine::Ranlux64Engine ( int  rowIndex,
int  colIndex,
int  lxr 
)

Definition at line 139 of file Ranlux64Engine.cc.

141{
142 luxury = lux;
143 int cycle = std::abs(int(rowIndex/maxIndex));
144 int row = std::abs(int(rowIndex%maxIndex));
145 long mask = (( cycle & 0x000007ff ) << 20 );
146 long seedlist[2];
147 HepRandom::getTheTableSeeds( seedlist, row );
148 seedlist[0] ^= mask;
149 seedlist[1]= 0;
150 setSeeds(seedlist, lux);
151}

◆ ~Ranlux64Engine()

CLHEP::Ranlux64Engine::~Ranlux64Engine ( )
virtual

Definition at line 159 of file Ranlux64Engine.cc.

159{}

Member Function Documentation

◆ beginTag()

std::string CLHEP::Ranlux64Engine::beginTag ( )
static

Definition at line 682 of file Ranlux64Engine.cc.

682 {
683 return "Ranlux64Engine-begin";
684}

◆ engineName()

static std::string CLHEP::Ranlux64Engine::engineName ( )
inlinestatic

Definition at line 93 of file Ranlux64Engine.h.

93{return "Ranlux64Engine";}

Referenced by restoreStatus().

◆ flat()

double CLHEP::Ranlux64Engine::flat ( )
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 161 of file Ranlux64Engine.cc.

161 {
162 // Luscher improves the speed by computing several numbers in a shot,
163 // in a manner similar to that of the Tausworth in DualRand or the Hurd
164 // engines. Thus, the real work is done in update(). Here we merely ensure
165 // that zero, which the algorithm can produce, is never returned by flat().
166
167 if (index <= 0) update();
168 return randoms[--index] + twoToMinus_49();
169}
static double twoToMinus_49()

Referenced by check_sequence(), flatArray(), main(), and valid_range().

◆ flatArray()

void CLHEP::Ranlux64Engine::flatArray ( const int  size,
double vect 
)
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 378 of file Ranlux64Engine.cc.

378 {
379 for( int i=0; i < size; ++i ) {
380 vect[i] = flat();
381 }
382}

◆ get() [1/2]

bool CLHEP::Ranlux64Engine::get ( const std::vector< unsigned long > &  v)
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 728 of file Ranlux64Engine.cc.

728 {
729 if ((v[0] & 0xffffffffUL) != engineIDulong<Ranlux64Engine>()) {
730 std::cerr <<
731 "\nRanlux64Engine get:state vector has wrong ID word - state unchanged\n";
732 return false;
733 }
734 return getState(v);
735}
virtual std::istream & getState(std::istream &is)

◆ get() [2/2]

std::istream & CLHEP::Ranlux64Engine::get ( std::istream &  is)
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 664 of file Ranlux64Engine.cc.

665{
666 char beginMarker [MarkerLen];
667 is >> std::ws;
668 is.width(MarkerLen); // causes the next read to the char* to be <=
669 // that many bytes, INCLUDING A TERMINATION \0
670 // (Stroustrup, section 21.3.2)
671 is >> beginMarker;
672 if (strcmp(beginMarker,"Ranlux64Engine-begin")) {
673 is.clear(std::ios::badbit | is.rdstate());
674 std::cerr << "\nInput stream mispositioned or"
675 << "\nRanlux64Engine state description missing or"
676 << "\nwrong engine type found." << std::endl;
677 return is;
678 }
679 return getState(is);
680}

◆ getLuxury()

int CLHEP::Ranlux64Engine::getLuxury ( ) const
inline

Definition at line 84 of file Ranlux64Engine.h.

84{ return luxury; }

◆ getState() [1/2]

bool CLHEP::Ranlux64Engine::getState ( const std::vector< unsigned long > &  v)
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 737 of file Ranlux64Engine.cc.

737 {
738 if (v.size() != VECTOR_STATE_SIZE ) {
739 std::cerr <<
740 "\nRanlux64Engine get:state vector has wrong length - state unchanged\n";
741 return false;
742 }
743 std::vector<unsigned long> t(2);
744 for (int i=0; i<12; ++i) {
745 t[0] = v[2*i+1]; t[1] = v[2*i+2];
746 randoms[i] = DoubConv::longs2double(t);
747 }
748 t[0] = v[25]; t[1] = v[26];
749 carry = DoubConv::longs2double(t);
750 index = (int)v[27];
751 luxury = (int)v[28];
752 pDiscard = (int)v[29];
753 return true;
754}
static double longs2double(const std::vector< unsigned long > &v)
Definition: DoubConv.cc:110
static const unsigned int VECTOR_STATE_SIZE

◆ getState() [2/2]

std::istream & CLHEP::Ranlux64Engine::getState ( std::istream &  is)
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 686 of file Ranlux64Engine.cc.

687{
688 if ( possibleKeywordInput ( is, "Uvec", theSeed ) ) {
689 std::vector<unsigned long> v;
690 unsigned long uu;
691 for (unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
692 is >> uu;
693 if (!is) {
694 is.clear(std::ios::badbit | is.rdstate());
695 std::cerr << "\nRanlux64Engine state (vector) description improper."
696 << "\ngetState() has failed."
697 << "\nInput stream is probably mispositioned now." << std::endl;
698 return is;
699 }
700 v.push_back(uu);
701 }
702 getState(v);
703 return (is);
704 }
705
706// is >> theSeed; Removed, encompassed by possibleKeywordInput()
707
708 char endMarker [MarkerLen];
709 for (int i=0; i<12; ++i) {
710 is >> randoms[i];
711 }
712 is >> carry; is >> index;
713 is >> luxury; is >> pDiscard;
714 pDozens = pDiscard / 12;
715 endIters = pDiscard % 12;
716 is >> std::ws;
717 is.width(MarkerLen);
718 is >> endMarker;
719 if (strcmp(endMarker,"Ranlux64Engine-end")) {
720 is.clear(std::ios::badbit | is.rdstate());
721 std::cerr << "\nRanlux64Engine state description incomplete."
722 << "\nInput stream is probably mispositioned now." << std::endl;
723 return is;
724 }
725 return is;
726}
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
Definition: RandomEngine.h:168

Referenced by get(), getState(), and restoreStatus().

◆ name()

std::string CLHEP::Ranlux64Engine::name ( ) const
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 107 of file Ranlux64Engine.cc.

107{return "Ranlux64Engine";}

◆ put() [1/2]

std::vector< unsigned long > CLHEP::Ranlux64Engine::put ( ) const
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 648 of file Ranlux64Engine.cc.

648 {
649 std::vector<unsigned long> v;
650 v.push_back (engineIDulong<Ranlux64Engine>());
651 std::vector<unsigned long> t;
652 for (int i=0; i<12; ++i) {
653 t = DoubConv::dto2longs(randoms[i]);
654 v.push_back(t[0]); v.push_back(t[1]);
655 }
656 t = DoubConv::dto2longs(carry);
657 v.push_back(t[0]); v.push_back(t[1]);
658 v.push_back(static_cast<unsigned long>(index));
659 v.push_back(static_cast<unsigned long>(luxury));
660 v.push_back(static_cast<unsigned long>(pDiscard));
661 return v;
662}
static std::vector< unsigned long > dto2longs(double d)
Definition: DoubConv.cc:94

Referenced by put(), and saveStatus().

◆ put() [2/2]

std::ostream & CLHEP::Ranlux64Engine::put ( std::ostream &  os) const
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 623 of file Ranlux64Engine.cc.

624{
625 char beginMarker[] = "Ranlux64Engine-begin";
626 os << beginMarker << "\nUvec\n";
627 std::vector<unsigned long> v = put();
628 for (unsigned int i=0; i<v.size(); ++i) {
629 os << v[i] << "\n";
630 }
631 return os;
632#ifdef REMOVED
633 char endMarker[] = "Ranlux64Engine-end";
634 long pr = os.precision(20);
635 os << " " << beginMarker << " ";
636 os << theSeed << " ";
637 for (int i=0; i<12; ++i) {
638 os << randoms[i] << std::endl;
639 }
640 os << carry << " " << index << " ";
641 os << luxury << " " << pDiscard << "\n";
642 os << endMarker << " ";
643 os.precision(pr);
644 return os;
645#endif
646}
std::vector< unsigned long > put() const

◆ restoreStatus()

void CLHEP::Ranlux64Engine::restoreStatus ( const char  filename[] = "Ranlux64.conf")
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 566 of file Ranlux64Engine.cc.

567{
568 std::ifstream inFile( filename, std::ios::in);
569 if (!checkFile ( inFile, filename, engineName(), "restoreStatus" )) {
570 std::cerr << " -- Engine state remains unchanged\n";
571 return;
572 }
573 if ( possibleKeywordInput ( inFile, "Uvec", theSeed ) ) {
574 std::vector<unsigned long> v;
575 unsigned long xin;
576 for (unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
577 inFile >> xin;
578 #ifdef TRACE_IO
579 std::cout << "ivec = " << ivec << " xin = " << xin << " ";
580 if (ivec%3 == 0) std::cout << "\n";
581 #endif
582 if (!inFile) {
583 inFile.clear(std::ios::badbit | inFile.rdstate());
584 std::cerr << "\nJamesRandom state (vector) description improper."
585 << "\nrestoreStatus has failed."
586 << "\nInput stream is probably mispositioned now." << std::endl;
587 return;
588 }
589 v.push_back(xin);
590 }
591 getState(v);
592 return;
593 }
594
595 if (!inFile.bad() && !inFile.eof()) {
596// inFile >> theSeed; removed -- encompased by possibleKeywordInput
597 for (int i=0; i<12; ++i) {
598 inFile >> randoms[i];
599 }
600 inFile >> carry; inFile >> index;
601 inFile >> luxury; inFile >> pDiscard;
602 pDozens = pDiscard / 12;
603 endIters = pDiscard % 12;
604 }
605}
static bool checkFile(std::istream &file, const std::string &filename, const std::string &classname, const std::string &methodname)
Definition: RandomEngine.cc:49
static std::string engineName()

◆ saveStatus()

void CLHEP::Ranlux64Engine::saveStatus ( const char  filename[] = "Ranlux64.conf") const
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 534 of file Ranlux64Engine.cc.

535{
536 std::ofstream outFile( filename, std::ios::out ) ;
537 if (!outFile.bad()) {
538 outFile << "Uvec\n";
539 std::vector<unsigned long> v = put();
540 #ifdef TRACE_IO
541 std::cout << "Result of v = put() is:\n";
542 #endif
543 for (unsigned int i=0; i<v.size(); ++i) {
544 outFile << v[i] << "\n";
545 #ifdef TRACE_IO
546 std::cout << v[i] << " ";
547 if (i%6==0) std::cout << "\n";
548 #endif
549 }
550 #ifdef TRACE_IO
551 std::cout << "\n";
552 #endif
553 }
554#ifdef REMOVED
555 if (!outFile.bad()) {
556 outFile << theSeed << std::endl;
557 for (int i=0; i<12; ++i) {
558 outFile <<std::setprecision(20) << randoms[i] << std::endl;
559 }
560 outFile << std::setprecision(20) << carry << " " << index << std::endl;
561 outFile << luxury << " " << pDiscard << std::endl;
562 }
563#endif
564}

◆ setSeed()

void CLHEP::Ranlux64Engine::setSeed ( long  seed,
int  lxr = 1 
)
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 384 of file Ranlux64Engine.cc.

384 {
385
386// The initialization is carried out using a Multiplicative
387// Congruential generator using formula constants of L'Ecuyer
388// as described in "A review of pseudorandom number generators"
389// (Fred James) published in Computer Physics Communications 60 (1990)
390// pages 329-344
391
392 const int ecuyer_a(53668);
393 const int ecuyer_b(40014);
394 const int ecuyer_c(12211);
395 const int ecuyer_d(2147483563);
396
397 const int lux_levels[3] = {109, 202, 397};
398 theSeed = seed;
399
400 if( (lux > 2)||(lux < 0) ){
401 pDiscard = (lux >= 12) ? (lux-12) : lux_levels[1];
402 }else{
403 pDiscard = lux_levels[luxury];
404 }
405 pDozens = pDiscard / 12;
406 endIters = pDiscard % 12;
407
408 long init_table[24];
409 long next_seed = seed;
410 long k_multiple;
411 int i;
412 next_seed &= 0xffffffff;
413 while( next_seed >= ecuyer_d ) {
414 next_seed -= ecuyer_d;
415 }
416
417 for(i = 0;i != 24;i++){
418 k_multiple = next_seed / ecuyer_a;
419 next_seed = ecuyer_b * (next_seed - k_multiple * ecuyer_a)
420 - k_multiple * ecuyer_c;
421 if(next_seed < 0) {
422 next_seed += ecuyer_d;
423 }
424 next_seed &= 0xffffffff;
425 init_table[i] = next_seed;
426 }
427 // are we on a 64bit machine?
428 if( sizeof(long) >= 8 ) {
429 int64_t topbits1, topbits2;
430#ifdef USING_VISUAL
431 topbits1 = ( (int64_t) seed >> 32) & 0xffff ;
432 topbits2 = ( (int64_t) seed >> 48) & 0xffff ;
433#else
434 topbits1 = detail::rshift<32>(seed) & 0xffff ;
435 topbits2 = detail::rshift<48>(seed) & 0xffff ;
436#endif
437 init_table[0] ^= topbits1;
438 init_table[2] ^= topbits2;
439 //std::cout << " init_table[0] " << init_table[0] << " from " << topbits1 << std::endl;
440 //std::cout << " init_table[2] " << init_table[2] << " from " << topbits2 << std::endl;
441 }
442
443 for(i = 0;i < 12; i++){
444 randoms[i] = (init_table[2*i ] ) * 2.0 * twoToMinus_32() +
445 (init_table[2*i+1] >> 15) * twoToMinus_48();
446 //if( randoms[i] < 0. || randoms[i] > 1. ) {
447 //std::cout << "setSeed: init_table " << init_table[2*i ] << std::endl;
448 //std::cout << "setSeed: init_table " << init_table[2*i+1] << std::endl;
449 //std::cout << "setSeed: random " << i << " is " << randoms[i] << std::endl;
450 //}
451 }
452
453 carry = 0.0;
454 if ( randoms[11] == 0. ) carry = twoToMinus_48();
455 // Perform an update before returning the first random number.
456 index = -1;
457
458} // setSeed()
static double twoToMinus_32()
static double twoToMinus_48()

Referenced by check_sequence(), setSeeds(), and valid_range().

◆ setSeeds()

void CLHEP::Ranlux64Engine::setSeeds ( const long *  seeds,
int  lxr = 1 
)
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 460 of file Ranlux64Engine.cc.

460 {
461// old code only uses the first long in seeds
462// setSeed( *seeds ? *seeds : 32767, lux );
463// theSeeds = seeds;
464
465// using code from Ranlux - even those are 32bit seeds,
466// that is good enough to completely differentiate the sequences
467
468 const int ecuyer_a = 53668;
469 const int ecuyer_b = 40014;
470 const int ecuyer_c = 12211;
471 const int ecuyer_d = 2147483563;
472
473 const int lux_levels[3] = {109, 202, 397};
474 const long *seedptr;
475
476 theSeeds = seeds;
477 seedptr = seeds;
478
479 if(seeds == 0){
480 setSeed(theSeed,lux);
481 theSeeds = &theSeed;
482 return;
483 }
484
485 theSeed = *seeds;
486
487// number of additional random numbers that need to be 'thrown away'
488// every 24 numbers is set using luxury level variable.
489
490 if( (lux > 2)||(lux < 0) ){
491 pDiscard = (lux >= 12) ? (lux-12) : lux_levels[1];
492 }else{
493 pDiscard = lux_levels[luxury];
494 }
495 pDozens = pDiscard / 12;
496 endIters = pDiscard % 12;
497
498 long init_table[24];
499 long next_seed = *seeds;
500 long k_multiple;
501 int i;
502
503 for( i = 0;(i != 24)&&(*seedptr != 0);i++){
504 init_table[i] = *seedptr & 0xffffffff;
505 seedptr++;
506 }
507
508 if(i != 24){
509 next_seed = init_table[i-1];
510 for(;i != 24;i++){
511 k_multiple = next_seed / ecuyer_a;
512 next_seed = ecuyer_b * (next_seed - k_multiple * ecuyer_a)
513 - k_multiple * ecuyer_c;
514 if(next_seed < 0) {
515 next_seed += ecuyer_d;
516 }
517 next_seed &= 0xffffffff;
518 init_table[i] = next_seed;
519 }
520 }
521
522 for(i = 0;i < 12; i++){
523 randoms[i] = (init_table[2*i ] ) * 2.0 * twoToMinus_32() +
524 (init_table[2*i+1] >> 15) * twoToMinus_48();
525 }
526
527 carry = 0.0;
528 if ( randoms[11] == 0. ) carry = twoToMinus_48();
529 // Perform an update before returning the first random number.
530 index = -1;
531
532}
void setSeed(long seed, int lxr=1)

Referenced by Ranlux64Engine().

◆ showStatus()

void CLHEP::Ranlux64Engine::showStatus ( ) const
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 607 of file Ranlux64Engine.cc.

608{
609 std::cout << std::endl;
610 std::cout << "--------- Ranlux engine status ---------" << std::endl;
611 std::cout << " Initial seed = " << theSeed << std::endl;
612 std::cout << " randoms[] = ";
613 for (int i=0; i<12; ++i) {
614 std::cout << randoms[i] << std::endl;
615 }
616 std::cout << std::endl;
617 std::cout << " carry = " << carry << ", index = " << index << std::endl;
618 std::cout << " luxury = " << luxury << " pDiscard = "
619 << pDiscard << std::endl;
620 std::cout << "----------------------------------------" << std::endl;
621}

Member Data Documentation

◆ VECTOR_STATE_SIZE

const unsigned int CLHEP::Ranlux64Engine::VECTOR_STATE_SIZE = 30
static

Definition at line 99 of file Ranlux64Engine.h.

Referenced by getState(), and restoreStatus().


The documentation for this class was generated from the following files: