Geant4 Cross Reference |
>> 1 // $Id:$ 1 // -*- C++ -*- 2 // -*- C++ -*- 2 // 3 // 3 // ------------------------------------------- 4 // ----------------------------------------------------------------------- 4 // HEP Random 5 // HEP Random 5 // --- RanshiEngine --- 6 // --- RanshiEngine --- 6 // class header file 7 // class header file 7 // ------------------------------------------- 8 // ----------------------------------------------------------------------- 8 // 9 // 9 // 10 // 10 // The algorithm for this random engine was ta 11 // The algorithm for this random engine was taken from "F.Gutbrod, Comp. 11 // Phys. Comm. 87 (1995) 291-306". 12 // Phys. Comm. 87 (1995) 291-306". 12 // 13 // 13 // The algorithm can be imagined as a physical 14 // The algorithm can be imagined as a physical system as follows: Imagine 14 // 512 "black balls" each with their own uniqu 15 // 512 "black balls" each with their own unique spin, and positions char- 15 // acterized by disrete angles, where the spin 16 // acterized by disrete angles, where the spin is a 32-bit unsigned integer. 16 // A "red ball" collides based upon the angle 17 // A "red ball" collides based upon the angle determined by the last 8 bits 17 // of its spin, and the spin of the colliding 18 // of its spin, and the spin of the colliding ball is taken as the output 18 // random number. The spin of the colliding ba 19 // random number. The spin of the colliding ball is replaced then with the 19 // left circular shift of the black ball's spi 20 // left circular shift of the black ball's spin XOR'd with the red ball's 20 // spin. The black ball's old spin becomes the 21 // spin. The black ball's old spin becomes the red ball's. 21 // 22 // 22 // To avoid the traps presented, two measures 23 // To avoid the traps presented, two measures are taken: first, the red 23 // ball will oscillate between hitting the low 24 // ball will oscillate between hitting the lower half of the buffer on one 24 // turn and the upper half on another; second, 25 // turn and the upper half on another; second, the red ball's spin is 25 // incremented by a counter of the number of r 26 // incremented by a counter of the number of random numbers produced. 26 // 27 // 27 // The result is scaled to a double precision 28 // The result is scaled to a double precision floating point number to which 28 // is added another random double further scal 29 // is added another random double further scaled 2^(53-32) places to the 29 // right in order to ensure that the remaining 30 // right in order to ensure that the remaining bits of the result are not 30 // left empty due to the mere 32 bits represen 31 // left empty due to the mere 32 bits representation used internally. 31 32 32 // =========================================== 33 // ======================================================================= 33 // Ken Smith - Created: 9th June 1998 34 // Ken Smith - Created: 9th June 1998 34 // - Removed pow() from flat me 35 // - Removed pow() from flat method: 21st Jul 1998 35 // - Added conversion operators 36 // - Added conversion operators: 6th Aug 1998 36 // Mark Fischler Methods put, get for insta 37 // Mark Fischler Methods put, get for instance save/restore 12/8/04 37 // Mark Fischler methods for anonymous save 38 // Mark Fischler methods for anonymous save/restore 12/27/04 38 // =========================================== 39 // ======================================================================= 39 40 40 #ifndef HepRanshiEngine_h 41 #ifndef HepRanshiEngine_h 41 #define HepRanshiEngine_h 42 #define HepRanshiEngine_h 42 43 43 #include "CLHEP/Random/RandomEngine.h" 44 #include "CLHEP/Random/RandomEngine.h" 44 45 45 namespace CLHEP { 46 namespace CLHEP { 46 47 47 /** 48 /** 48 * @author 49 * @author 49 * @ingroup random 50 * @ingroup random 50 */ 51 */ 51 class RanshiEngine: public HepRandomEngine { 52 class RanshiEngine: public HepRandomEngine { 52 53 53 public: 54 public: 54 55 55 RanshiEngine(); 56 RanshiEngine(); 56 RanshiEngine(std::istream &is); 57 RanshiEngine(std::istream &is); 57 RanshiEngine(long seed); 58 RanshiEngine(long seed); 58 RanshiEngine(int rowIndex, int colIndex); 59 RanshiEngine(int rowIndex, int colIndex); 59 virtual ~RanshiEngine(); 60 virtual ~RanshiEngine(); 60 // Constructors and destructor 61 // Constructors and destructor 61 62 62 double flat(); 63 double flat(); 63 // Returns a pseudo random number between 64 // Returns a pseudo random number between 0 and 1 64 65 65 void flatArray(const int size, double* vec 66 void flatArray(const int size, double* vect); 66 // Fills the array "vect" of specified siz 67 // Fills the array "vect" of specified size with flat random values 67 68 68 void setSeed(long seed, int); 69 void setSeed(long seed, int); 69 // Sets the state of the algorithm accordi 70 // Sets the state of the algorithm according to seed. 70 71 71 void setSeeds(const long* seeds, int); 72 void setSeeds(const long* seeds, int); 72 // Sets the state of the algorithm accordi 73 // Sets the state of the algorithm according to the zero-terminated 73 // array of seeds. 74 // array of seeds. 74 75 75 void saveStatus(const char filename[] = "R 76 void saveStatus(const char filename[] = "RanshiEngine.conf") const; 76 // Saves on named file the current engine 77 // Saves on named file the current engine status 77 78 78 void restoreStatus(const char filename[] = 79 void restoreStatus(const char filename[] = "RanshiEngine.conf"); 79 // Reads from named file the last saved en 80 // Reads from named file the last saved engine status 80 // and restores it. 81 // and restores it. 81 82 82 void showStatus() const; 83 void showStatus() const; 83 // Dumps the engine status on the screen 84 // Dumps the engine status on the screen 84 85 85 operator double(); // Returns same a << 86 operator float(); // flat value, without worrying about filling bits 86 operator float(); // flat value, wi << 87 operator unsigned int(); // 32-bit flat value, quickest of all 87 operator unsigned int(); // 32-bit flat va << 88 88 << 89 virtual std::ostream & put (std::ostream & os) const; 89 virtual std::ostream & put (std::ostream & << 90 virtual std::istream & get (std::istream & is); 90 virtual std::istream & get (std::istream & << 91 static std::string beginTag ( ); 91 static std::string beginTag ( ); << 92 virtual std::istream & getState ( std::istream & is ); 92 virtual std::istream & getState ( std::ist << 93 93 << 94 std::string name() const; 94 std::string name() const; << 95 static std::string engineName() {return "RanshiEngine";} 95 static std::string engineName() {return "R << 96 96 << 97 std::vector<unsigned long> put () const; 97 std::vector<unsigned long> put () const; << 98 bool get (const std::vector<unsigned long> & v); 98 bool get (const std::vector<unsigned long> << 99 bool getState (const std::vector<unsigned long> & v); 99 bool getState (const std::vector<unsigned << 100 100 101 private: 101 private: 102 enum {numBuff = 512}; 102 enum {numBuff = 512}; 103 103 104 unsigned int halfBuff, numFlats; 104 unsigned int halfBuff, numFlats; 105 unsigned int buffer[numBuff]; 105 unsigned int buffer[numBuff]; 106 unsigned int redSpin; 106 unsigned int redSpin; 107 107 108 static const unsigned int VECTOR_STATE_SIZ 108 static const unsigned int VECTOR_STATE_SIZE = numBuff + 4; 109 109 110 }; // RanshiEngine 110 }; // RanshiEngine 111 111 112 } // namespace CLHEP 112 } // namespace CLHEP 113 113 114 #endif // HepRanshiEngine_h 114 #endif // HepRanshiEngine_h 115 115