CLHEP VERSION Reference Documentation
CLHEP Home Page CLHEP Documentation CLHEP Bug Reports |
00001 // $Id: RanshiEngine.h,v 1.5 2010/06/16 17:24:53 garren Exp $ 00002 // -*- C++ -*- 00003 // 00004 // ----------------------------------------------------------------------- 00005 // HEP Random 00006 // --- RanshiEngine --- 00007 // class header file 00008 // ----------------------------------------------------------------------- 00009 // 00010 // 00011 // The algorithm for this random engine was taken from "F.Gutbrod, Comp. 00012 // Phys. Comm. 87 (1995) 291-306". 00013 // 00014 // The algorithm can be imagined as a physical system as follows: Imagine 00015 // 512 "black balls" each with their own unique spin, and positions char- 00016 // acterized by disrete angles, where the spin is a 32-bit unsigned integer. 00017 // A "red ball" collides based upon the angle determined by the last 8 bits 00018 // of its spin, and the spin of the colliding ball is taken as the output 00019 // random number. The spin of the colliding ball is replaced then with the 00020 // left circular shift of the black ball's spin XOR'd with the red ball's 00021 // spin. The black ball's old spin becomes the red ball's. 00022 // 00023 // To avoid the traps presented, two measures are taken: first, the red 00024 // ball will oscillate between hitting the lower half of the buffer on one 00025 // turn and the upper half on another; second, the red ball's spin is 00026 // incremented by a counter of the number of random numbers produced. 00027 // 00028 // The result is scaled to a double precision floating point number to which 00029 // is added another random double further scaled 2^(53-32) places to the 00030 // right in order to ensure that the remaining bits of the result are not 00031 // left empty due to the mere 32 bits representation used internally. 00032 00033 // ======================================================================= 00034 // Ken Smith - Created: 9th June 1998 00035 // - Removed pow() from flat method: 21st Jul 1998 00036 // - Added conversion operators: 6th Aug 1998 00037 // Mark Fischler Methods put, get for instance save/restore 12/8/04 00038 // Mark Fischler methods for anonymous save/restore 12/27/04 00039 // ======================================================================= 00040 00041 #ifndef HepRanshiEngine_h 00042 #define HepRanshiEngine_h 00043 00044 #include "CLHEP/Random/defs.h" 00045 #include "CLHEP/Random/RandomEngine.h" 00046 00047 namespace CLHEP { 00048 00053 class RanshiEngine: public HepRandomEngine { 00054 00055 public: 00056 00057 RanshiEngine(); 00058 RanshiEngine(std::istream &is); 00059 RanshiEngine(long seed); 00060 RanshiEngine(int rowIndex, int colIndex); 00061 virtual ~RanshiEngine(); 00062 // Constructors and destructor 00063 00064 double flat(); 00065 // Returns a pseudo random number between 0 and 1 00066 00067 void flatArray(const int size, double* vect); 00068 // Fills the array "vect" of specified size with flat random values 00069 00070 void setSeed(long seed, int); 00071 // Sets the state of the algorithm according to seed. 00072 00073 void setSeeds(const long* seeds, int); 00074 // Sets the state of the algorithm according to the zero-terminated 00075 // array of seeds. 00076 00077 void saveStatus(const char filename[] = "RanshiEngine.conf") const; 00078 // Saves on named file the current engine status 00079 00080 void restoreStatus(const char filename[] = "RanshiEngine.conf"); 00081 // Reads from named file the last saved engine status 00082 // and restores it. 00083 00084 void showStatus() const; 00085 // Dumps the engine status on the screen 00086 00087 operator float(); // flat value, without worrying about filling bits 00088 operator unsigned int(); // 32-bit flat value, quickest of all 00089 00090 virtual std::ostream & put (std::ostream & os) const; 00091 virtual std::istream & get (std::istream & is); 00092 static std::string beginTag ( ); 00093 virtual std::istream & getState ( std::istream & is ); 00094 00095 std::string name() const; 00096 static std::string engineName() {return "RanshiEngine";} 00097 00098 std::vector<unsigned long> put () const; 00099 bool get (const std::vector<unsigned long> & v); 00100 bool getState (const std::vector<unsigned long> & v); 00101 00102 private: 00103 static int numEngines; 00104 enum {numBuff = 512}; 00105 00106 unsigned int halfBuff, numFlats; 00107 unsigned int buffer[numBuff]; 00108 unsigned int redSpin; 00109 00110 static const unsigned int VECTOR_STATE_SIZE = numBuff + 4; 00111 00112 }; // RanshiEngine 00113 00114 } // namespace CLHEP 00115 00116 #ifdef ENABLE_BACKWARDS_COMPATIBILITY 00117 // backwards compatibility will be enabled ONLY in CLHEP 1.9 00118 using namespace CLHEP; 00119 #endif 00120 00121 #endif // HepRanshiEngine_h