CLHEP 2.0.4.7 Reference Documentation
CLHEP Home Page CLHEP Documentation CLHEP Bug Reports |
00001 // $Id: TripleRand.h,v 1.3.4.2 2005/04/15 16:32:53 garren Exp $ 00002 // -*- C++ -*- 00003 // 00004 // ----------------------------------------------------------------------- 00005 // Hep Random 00006 // --- TripleRand --- 00007 // class header file 00008 // ----------------------------------------------------------------------- 00009 // A canopy pseudo-random number generator. Using the Tausworthe 00010 // exclusive-or shift register, a simple Integer Coungruence generator, and 00011 // the Hurd 288 total bit shift register, all XOR'd with each other, we 00012 // provide an engine that should be a fairly good "mother" generator. 00013 // 00014 // This is similar to DualRand, with the addition of the Hurd288Engine. 00015 // From DualRand, we have the following: 00016 // Exclusive or of a feedback shift register and integer congruence 00017 // random number generator. The feedback shift register uses offsets 00018 // 127 and 97. The integer congruence generator uses a different 00019 // multiplier for each stream. The multipliers are chosen to give 00020 // full period and maximum "potency" for modulo 2^32. The period of 00021 // the combined random number generator is 2^159 - 2^32, and the 00022 // sequences are different for each stream (not just started in a 00023 // different place). 00024 // The above is then amended to also add in the exclusive or of the 00025 // 288-total bit Hurd engine which in this case is a series of 32 00026 // interconnected 9-bit shift registers, with the newest bit of each register 00027 // formed by the XOR of the previous bit and some bit b-d from a previous 00028 // register where d is chosen to create a primitive polynomial to maximize 00029 // the period. 00030 // ======================================================================= 00031 // Ken Smith - Initial draft started: 23rd Jul 1998 00032 // - Added conversion operators: 6th Aug 1998 00033 // M Fischler - Big merge with CLHEP 13 May 1999 00034 // - Elimination of unused Taus() and Cong() accessors 00035 // Mark Fischler Methods put, get for instance save/restore 12/8/04 00036 // Mark Fischler methods for anonymous save/restore 12/27/04 00037 // ======================================================================= 00038 00039 #ifndef TripleRand_h 00040 #define TripleRand_h 00041 00042 #include "CLHEP/Random/defs.h" 00043 #include "CLHEP/Random/RandomEngine.h" 00044 #include "CLHEP/Random/Hurd288Engine.h" 00045 00046 namespace CLHEP { 00047 00052 class TripleRand: public HepRandomEngine { 00053 00054 public: 00055 00056 TripleRand(); 00057 TripleRand( long seed ); 00058 TripleRand( std::istream & is ); 00059 TripleRand( int rowIndex, int colIndex ); 00060 virtual ~TripleRand(); 00061 // Constructors and destructor 00062 00063 TripleRand( const TripleRand & p ); 00064 TripleRand & operator=( const TripleRand & p ); 00065 // Copy constructor and operator= 00066 00067 double flat(); 00068 // Returns a pseudo random number between 0 and 1 00069 // (excluding the end points) 00070 00071 void flatArray( const int size, double * vect ); 00072 // Fills an array "vect" of specified size with flat random values. 00073 00074 void setSeed( long seed, int ); 00075 // Sets the state of the algorithm according to seed. 00076 00077 void setSeeds( const long * seeds, int ); 00078 // Sets the state of the algorithm according to the zero-terminated 00079 // array of seeds. 00080 00081 void saveStatus( const char filename[] = "TripleRand.conf" ) const; 00082 // Saves on named file the current engine status. 00083 00084 void restoreStatus( const char filename[] = "TripleRand.conf" ); 00085 // Reads from named file the last saved engine status and restores it. 00086 00087 void showStatus() const; 00088 // Dumps the current engine status on the screen. 00089 00090 operator float(); // flat value, without worrying about filling bits 00091 operator unsigned int(); // 32-bit flat value, quickest of all 00092 00093 virtual std::ostream & put (std::ostream & os) const; 00094 virtual std::istream & get (std::istream & is); 00095 static std::string beginTag ( ); 00096 virtual std::istream & getState ( std::istream & is ); 00097 00098 std::string name() const; 00099 static std::string engineName() {return "TripleRand";} 00100 00101 std::vector<unsigned long> put () const; 00102 bool get (const std::vector<unsigned long> & v); 00103 bool getState (const std::vector<unsigned long> & v); 00104 00105 static const unsigned int VECTOR_STATE_SIZE = 20; 00106 00107 private: 00108 00109 static int numEngines; 00110 00115 class Tausworthe { 00116 public: 00117 00118 Tausworthe(); 00119 Tausworthe(unsigned int seed); 00120 00121 operator unsigned int(); 00122 00123 void put( std::ostream & os ) const; 00124 void put(std::vector<unsigned long> & v) const; 00125 void get( std::istream & is ); 00126 bool get(std::vector<unsigned long>::const_iterator & iv); 00127 00128 private: 00129 00130 int wordIndex; 00131 unsigned int words[4]; 00132 }; // Tausworthe 00133 00138 class IntegerCong { 00139 public: 00140 00141 IntegerCong(); 00142 IntegerCong(unsigned int seed, int streamNumber); 00143 00144 operator unsigned int(); 00145 00146 void put( std::ostream & os ) const; 00147 void put(std::vector<unsigned long> & v) const; 00148 void get( std::istream & is ); 00149 bool get(std::vector<unsigned long>::const_iterator & iv); 00150 00151 private: 00152 00153 unsigned int state, multiplier, addend; 00154 }; // IntegerCong 00155 00156 static double twoToMinus_32; 00157 static double twoToMinus_53; 00158 static double nearlyTwoToMinus_54; 00159 void powersOfTwo(); 00160 00161 Hurd288Engine & Hurd(); // retrieve the constituent engine for input 00162 00163 const Tausworthe & ConstTaus() const; // Same as above 00164 const IntegerCong & ConstCong() const; // necessary for 00165 const Hurd288Engine & ConstHurd() const; // output 00166 00167 Tausworthe tausworthe; // Instances of each of the 00168 IntegerCong integerCong; // three engines that combine to make 00169 Hurd288Engine hurd; // one TripleRand instance 00170 00171 }; // TripleRand 00172 00173 } // namespace CLHEP 00174 00175 #ifdef ENABLE_BACKWARDS_COMPATIBILITY 00176 // backwards compatibility will be enabled ONLY in CLHEP 1.9 00177 using namespace CLHEP; 00178 #endif 00179 00180 #endif // TripleRand_h