Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Static Public Member Functions | Static Public Attributes
CLHEP::Ranlux64Engine Class Reference

#include <Ranlux64Engine.h>

Inheritance diagram for CLHEP::Ranlux64Engine:
CLHEP::HepRandomEngine

Public Member Functions

 Ranlux64Engine (std::istream &is)
 
 Ranlux64Engine ()
 
 Ranlux64Engine (long seed, int lux=1)
 
 Ranlux64Engine (int rowIndex, int colIndex, int lux)
 
virtual ~Ranlux64Engine ()
 
double flat ()
 
void flatArray (const int size, double *vect)
 
void setSeed (long seed, int lux=1)
 
void setSeeds (const long *seeds, int lux=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)
 
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 49 of file Ranlux64Engine.h.

Constructor & Destructor Documentation

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

Definition at line 144 of file Ranlux64Engine.cc.

145 : HepRandomEngine()
146 {
147  is >> *this;
148 }
CLHEP::Ranlux64Engine::Ranlux64Engine ( )

Definition at line 101 of file Ranlux64Engine.cc.

References CLHEP::HepRandom::getTheTableSeeds(), and setSeeds().

102 : HepRandomEngine()
103 {
104  luxury = 1;
105  int cycle = std::abs(int(numEngines/maxIndex));
106  int curIndex = std::abs(int(numEngines%maxIndex));
107  numEngines +=1;
108  long mask = ((cycle & 0x007fffff) << 8);
109  long seedlist[2];
110  HepRandom::getTheTableSeeds( seedlist, curIndex );
111  seedlist[0] ^= mask;
112  seedlist[1] = 0;
113 
114  setSeeds(seedlist, luxury);
115  advance ( 8 ); // Discard some iterations and ensure that
116  // this sequence won't match one where seeds
117  // were provided.
118 }
void setSeeds(const long *seeds, int lux=1)
static void getTheTableSeeds(long *seeds, int index)
Definition: Random.cc:151
CLHEP::Ranlux64Engine::Ranlux64Engine ( long  seed,
int  lux = 1 
)

Definition at line 120 of file Ranlux64Engine.cc.

References setSeeds().

121 : HepRandomEngine()
122 {
123  luxury = lux;
124  long seedlist[2]={seed,0};
125  setSeeds(seedlist, lux);
126  advance ( 2*lux + 1 ); // Discard some iterations to use a different
127  // point in the sequence.
128 }
void setSeeds(const long *seeds, int lux=1)
CLHEP::Ranlux64Engine::Ranlux64Engine ( int  rowIndex,
int  colIndex,
int  lux 
)

Definition at line 130 of file Ranlux64Engine.cc.

References CLHEP::HepRandom::getTheTableSeeds(), and setSeeds().

131 : HepRandomEngine()
132 {
133  luxury = lux;
134  int cycle = std::abs(int(rowIndex/maxIndex));
135  int row = std::abs(int(rowIndex%maxIndex));
136  long mask = (( cycle & 0x000007ff ) << 20 );
137  long seedlist[2];
138  HepRandom::getTheTableSeeds( seedlist, row );
139  seedlist[0] ^= mask;
140  seedlist[1]= 0;
141  setSeeds(seedlist, lux);
142 }
void setSeeds(const long *seeds, int lux=1)
static void getTheTableSeeds(long *seeds, int index)
Definition: Random.cc:151
CLHEP::Ranlux64Engine::~Ranlux64Engine ( )
virtual

Definition at line 150 of file Ranlux64Engine.cc.

150 {}

Member Function Documentation

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

Definition at line 633 of file Ranlux64Engine.cc.

633  {
634  return "Ranlux64Engine-begin";
635 }
static std::string CLHEP::Ranlux64Engine::engineName ( )
inlinestatic

Definition at line 92 of file Ranlux64Engine.h.

Referenced by restoreStatus().

92 {return "Ranlux64Engine";}
double CLHEP::Ranlux64Engine::flat ( )
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 152 of file Ranlux64Engine.cc.

References CLHEP::HepRandomEngine::twoToMinus_49().

Referenced by flatArray().

152  {
153  // Luscher improves the speed by computing several numbers in a shot,
154  // in a manner similar to that of the Tausworth in DualRand or the Hurd
155  // engines. Thus, the real work is done in update(). Here we merely ensure
156  // that zero, which the algorithm can produce, is never returned by flat().
157 
158  if (index <= 0) update();
159  return randoms[--index] + twoToMinus_49();
160 }
static double twoToMinus_49()
void CLHEP::Ranlux64Engine::flatArray ( const int  size,
double *  vect 
)
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 369 of file Ranlux64Engine.cc.

References flat().

369  {
370  for( int i=0; i < size; ++i ) {
371  vect[i] = flat();
372  }
373 }
std::istream & CLHEP::Ranlux64Engine::get ( std::istream &  is)
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 615 of file Ranlux64Engine.cc.

References getState().

616 {
617  char beginMarker [MarkerLen];
618  is >> std::ws;
619  is.width(MarkerLen); // causes the next read to the char* to be <=
620  // that many bytes, INCLUDING A TERMINATION \0
621  // (Stroustrup, section 21.3.2)
622  is >> beginMarker;
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;
628  return is;
629  }
630  return getState(is);
631 }
virtual std::istream & getState(std::istream &is)
bool CLHEP::Ranlux64Engine::get ( const std::vector< unsigned long > &  v)
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 679 of file Ranlux64Engine.cc.

References getState().

679  {
680  if ((v[0] & 0xffffffffUL) != engineIDulong<Ranlux64Engine>()) {
681  std::cerr <<
682  "\nRanlux64Engine get:state vector has wrong ID word - state unchanged\n";
683  return false;
684  }
685  return getState(v);
686 }
virtual std::istream & getState(std::istream &is)
int CLHEP::Ranlux64Engine::getLuxury ( ) const
inline

Definition at line 83 of file Ranlux64Engine.h.

83 { return luxury; }
std::istream & CLHEP::Ranlux64Engine::getState ( std::istream &  is)
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 637 of file Ranlux64Engine.cc.

References CLHEP::possibleKeywordInput(), CLHEP::HepRandomEngine::theSeed, test::v, and VECTOR_STATE_SIZE.

Referenced by get(), and restoreStatus().

638 {
639  if ( possibleKeywordInput ( is, "Uvec", theSeed ) ) {
640  std::vector<unsigned long> v;
641  unsigned long uu;
642  for (unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
643  is >> uu;
644  if (!is) {
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;
649  return is;
650  }
651  v.push_back(uu);
652  }
653  getState(v);
654  return (is);
655  }
656 
657 // is >> theSeed; Removed, encompassed by possibleKeywordInput()
658 
659  char endMarker [MarkerLen];
660  for (int i=0; i<12; ++i) {
661  is >> randoms[i];
662  }
663  is >> carry; is >> index;
664  is >> luxury; is >> pDiscard;
665  pDozens = pDiscard / 12;
666  endIters = pDiscard % 12;
667  is >> std::ws;
668  is.width(MarkerLen);
669  is >> endMarker;
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;
674  return is;
675  }
676  return is;
677 }
static const unsigned int VECTOR_STATE_SIZE
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
Definition: RandomEngine.h:167
virtual std::istream & getState(std::istream &is)
bool CLHEP::Ranlux64Engine::getState ( const std::vector< unsigned long > &  v)
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 688 of file Ranlux64Engine.cc.

References CLHEP::DoubConv::longs2double(), and VECTOR_STATE_SIZE.

688  {
689  if (v.size() != VECTOR_STATE_SIZE ) {
690  std::cerr <<
691  "\nRanlux64Engine get:state vector has wrong length - state unchanged\n";
692  return false;
693  }
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];
697  randoms[i] = DoubConv::longs2double(t);
698  }
699  t[0] = v[25]; t[1] = v[26];
700  carry = DoubConv::longs2double(t);
701  index = v[27];
702  luxury = v[28];
703  pDiscard = v[29];
704  return true;
705 }
static const unsigned int VECTOR_STATE_SIZE
static double longs2double(const std::vector< unsigned long > &v)
Definition: DoubConv.cc:114
std::string CLHEP::Ranlux64Engine::name ( ) const
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 99 of file Ranlux64Engine.cc.

99 {return "Ranlux64Engine";}
std::ostream & CLHEP::Ranlux64Engine::put ( std::ostream &  os) const
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 588 of file Ranlux64Engine.cc.

References put(), and test::v.

589 {
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) {
594  os << v[i] << "\n";
595  }
596  return os;
597 }
std::vector< unsigned long > put() const
std::vector< unsigned long > CLHEP::Ranlux64Engine::put ( ) const
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 599 of file Ranlux64Engine.cc.

References CLHEP::DoubConv::dto2longs(), and test::v.

Referenced by put(), and saveStatus().

599  {
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) {
604  t = DoubConv::dto2longs(randoms[i]);
605  v.push_back(t[0]); v.push_back(t[1]);
606  }
607  t = DoubConv::dto2longs(carry);
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));
612  return v;
613 }
static std::vector< unsigned long > dto2longs(double d)
Definition: DoubConv.cc:98
void CLHEP::Ranlux64Engine::restoreStatus ( const char  filename[] = "Ranlux64.conf")
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 535 of file Ranlux64Engine.cc.

References CLHEP::HepRandomEngine::checkFile(), engineName(), getState(), CLHEP::possibleKeywordInput(), CLHEP::HepRandomEngine::theSeed, test::v, and VECTOR_STATE_SIZE.

536 {
537  std::ifstream inFile( filename, std::ios::in);
538  if (!checkFile ( inFile, filename, engineName(), "restoreStatus" )) {
539  std::cerr << " -- Engine state remains unchanged\n";
540  return;
541  }
542  if ( possibleKeywordInput ( inFile, "Uvec", theSeed ) ) {
543  std::vector<unsigned long> v;
544  unsigned long xin;
545  for (unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
546  inFile >> xin;
547  if (!inFile) {
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;
552  return;
553  }
554  v.push_back(xin);
555  }
556  getState(v);
557  return;
558  }
559 
560  if (!inFile.bad() && !inFile.eof()) {
561 // inFile >> theSeed; removed -- encompased by possibleKeywordInput
562  for (int i=0; i<12; ++i) {
563  inFile >> randoms[i];
564  }
565  inFile >> carry; inFile >> index;
566  inFile >> luxury; inFile >> pDiscard;
567  pDozens = pDiscard / 12;
568  endIters = pDiscard % 12;
569  }
570 }
static bool checkFile(std::istream &file, const std::string &filename, const std::string &classname, const std::string &methodname)
Definition: RandomEngine.cc:45
static std::string engineName()
static const unsigned int VECTOR_STATE_SIZE
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
Definition: RandomEngine.h:167
virtual std::istream & getState(std::istream &is)
void CLHEP::Ranlux64Engine::saveStatus ( const char  filename[] = "Ranlux64.conf") const
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 523 of file Ranlux64Engine.cc.

References outFile, put(), and test::v.

524 {
525  std::ofstream outFile( filename, std::ios::out ) ;
526  if (!outFile.bad()) {
527  outFile << "Uvec\n";
528  std::vector<unsigned long> v = put();
529  for (unsigned int i=0; i<v.size(); ++i) {
530  outFile << v[i] << "\n";
531  }
532  }
533 }
std::ofstream outFile
Definition: GammaRayTel.cc:68
std::vector< unsigned long > put() const
void CLHEP::Ranlux64Engine::setSeed ( long  seed,
int  lux = 1 
)
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 375 of file Ranlux64Engine.cc.

References CLHEP::HepRandomEngine::theSeed, CLHEP::HepRandomEngine::twoToMinus_32(), and CLHEP::HepRandomEngine::twoToMinus_48().

Referenced by setSeeds().

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

Implements CLHEP::HepRandomEngine.

Definition at line 450 of file Ranlux64Engine.cc.

References setSeed(), CLHEP::HepRandomEngine::theSeed, CLHEP::HepRandomEngine::theSeeds, CLHEP::HepRandomEngine::twoToMinus_32(), and CLHEP::HepRandomEngine::twoToMinus_48().

Referenced by Ranlux64Engine().

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

Implements CLHEP::HepRandomEngine.

Definition at line 572 of file Ranlux64Engine.cc.

References CLHEP::HepRandomEngine::theSeed.

573 {
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;
580  }
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;
586 }

Field Documentation

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

Definition at line 98 of file Ranlux64Engine.h.

Referenced by getState(), and restoreStatus().


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