CLHEP VERSION Reference Documentation
CLHEP Home Page CLHEP Documentation CLHEP Bug Reports |
00001 // $Id: TripleRand.h,v 1.5 2010/06/16 17:24: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 double flat(); 00064 // Returns a pseudo random number between 0 and 1 00065 // (excluding the end points) 00066 00067 void flatArray( const int size, double * vect ); 00068 // Fills an 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[] = "TripleRand.conf" ) const; 00078 // Saves on named file the current engine status. 00079 00080 void restoreStatus( const char filename[] = "TripleRand.conf" ); 00081 // Reads from named file the last saved engine status and restores it. 00082 00083 void showStatus() const; 00084 // Dumps the current engine status on the screen. 00085 00086 operator float(); // flat value, without worrying about filling bits 00087 operator unsigned int(); // 32-bit flat value, quickest of all 00088 00089 virtual std::ostream & put (std::ostream & os) const; 00090 virtual std::istream & get (std::istream & is); 00091 static std::string beginTag ( ); 00092 virtual std::istream & getState ( std::istream & is ); 00093 00094 std::string name() const; 00095 static std::string engineName() {return "TripleRand";} 00096 00097 std::vector<unsigned long> put () const; 00098 bool get (const std::vector<unsigned long> & v); 00099 bool getState (const std::vector<unsigned long> & v); 00100 00101 static const unsigned int VECTOR_STATE_SIZE = 20; 00102 00103 private: 00104 00105 static int numEngines; 00106 00111 class Tausworthe { 00112 public: 00113 00114 Tausworthe(); 00115 Tausworthe(unsigned int seed); 00116 00117 operator unsigned int(); 00118 00119 void put( std::ostream & os ) const; 00120 void put(std::vector<unsigned long> & v) const; 00121 void get( std::istream & is ); 00122 bool get(std::vector<unsigned long>::const_iterator & iv); 00123 00124 private: 00125 00126 int wordIndex; 00127 unsigned int words[4]; 00128 }; // Tausworthe 00129 00134 class IntegerCong { 00135 public: 00136 00137 IntegerCong(); 00138 IntegerCong(unsigned int seed, int streamNumber); 00139 00140 operator unsigned int(); 00141 00142 void put( std::ostream & os ) const; 00143 void put(std::vector<unsigned long> & v) const; 00144 void get( std::istream & is ); 00145 bool get(std::vector<unsigned long>::const_iterator & iv); 00146 00147 private: 00148 00149 unsigned int state, multiplier, addend; 00150 }; // IntegerCong 00151 00152 Hurd288Engine & Hurd(); // retrieve the constituent engine for input 00153 00154 const Tausworthe & ConstTaus() const; // Same as above 00155 const IntegerCong & ConstCong() const; // necessary for 00156 const Hurd288Engine & ConstHurd() const; // output 00157 00158 Tausworthe tausworthe; // Instances of each of the 00159 IntegerCong integerCong; // three engines that combine to make 00160 Hurd288Engine hurd; // one TripleRand instance 00161 00162 }; // TripleRand 00163 00164 } // namespace CLHEP 00165 00166 #ifdef ENABLE_BACKWARDS_COMPATIBILITY 00167 // backwards compatibility will be enabled ONLY in CLHEP 1.9 00168 using namespace CLHEP; 00169 #endif 00170 00171 #endif // TripleRand_h