CLHEP VERSION Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

Random/CLHEP/Random/TripleRand.h
Go to the documentation of this file.
1 // $Id: TripleRand.h,v 1.5 2010/06/16 17:24:53 garren Exp $
2 // -*- C++ -*-
3 //
4 // -----------------------------------------------------------------------
5 // Hep Random
6 // --- TripleRand ---
7 // class header file
8 // -----------------------------------------------------------------------
9 // A canopy pseudo-random number generator. Using the Tausworthe
10 // exclusive-or shift register, a simple Integer Coungruence generator, and
11 // the Hurd 288 total bit shift register, all XOR'd with each other, we
12 // provide an engine that should be a fairly good "mother" generator.
13 //
14 // This is similar to DualRand, with the addition of the Hurd288Engine.
15 // From DualRand, we have the following:
16 // Exclusive or of a feedback shift register and integer congruence
17 // random number generator. The feedback shift register uses offsets
18 // 127 and 97. The integer congruence generator uses a different
19 // multiplier for each stream. The multipliers are chosen to give
20 // full period and maximum "potency" for modulo 2^32. The period of
21 // the combined random number generator is 2^159 - 2^32, and the
22 // sequences are different for each stream (not just started in a
23 // different place).
24 // The above is then amended to also add in the exclusive or of the
25 // 288-total bit Hurd engine which in this case is a series of 32
26 // interconnected 9-bit shift registers, with the newest bit of each register
27 // formed by the XOR of the previous bit and some bit b-d from a previous
28 // register where d is chosen to create a primitive polynomial to maximize
29 // the period.
30 // =======================================================================
31 // Ken Smith - Initial draft started: 23rd Jul 1998
32 // - Added conversion operators: 6th Aug 1998
33 // M Fischler - Big merge with CLHEP 13 May 1999
34 // - Elimination of unused Taus() and Cong() accessors
35 // Mark Fischler Methods put, get for instance save/restore 12/8/04
36 // Mark Fischler methods for anonymous save/restore 12/27/04
37 // =======================================================================
38 
39 #ifndef TripleRand_h
40 #define TripleRand_h
41 
42 #include "CLHEP/Random/defs.h"
43 #include "CLHEP/Random/RandomEngine.h"
44 #include "CLHEP/Random/Hurd288Engine.h"
45 
46 namespace CLHEP {
47 
52 class TripleRand: public HepRandomEngine {
53 
54 public:
55 
56  TripleRand();
57  TripleRand( long seed );
58  TripleRand( std::istream & is );
59  TripleRand( int rowIndex, int colIndex );
60  virtual ~TripleRand();
61  // Constructors and destructor
62 
63  double flat();
64  // Returns a pseudo random number between 0 and 1
65  // (excluding the end points)
66 
67  void flatArray( const int size, double * vect );
68  // Fills an array "vect" of specified size with flat random values.
69 
70  void setSeed( long seed, int );
71  // Sets the state of the algorithm according to seed.
72 
73  void setSeeds( const long * seeds, int );
74  // Sets the state of the algorithm according to the zero-terminated
75  // array of seeds.
76 
77  void saveStatus( const char filename[] = "TripleRand.conf" ) const;
78  // Saves on named file the current engine status.
79 
80  void restoreStatus( const char filename[] = "TripleRand.conf" );
81  // Reads from named file the last saved engine status and restores it.
82 
83  void showStatus() const;
84  // Dumps the current engine status on the screen.
85 
86  operator float(); // flat value, without worrying about filling bits
87  operator unsigned int(); // 32-bit flat value, quickest of all
88 
89  virtual std::ostream & put (std::ostream & os) const;
90  virtual std::istream & get (std::istream & is);
91  static std::string beginTag ( );
92  virtual std::istream & getState ( std::istream & is );
93 
94  std::string name() const;
95  static std::string engineName() {return "TripleRand";}
96 
97  std::vector<unsigned long> put () const;
98  bool get (const std::vector<unsigned long> & v);
99  bool getState (const std::vector<unsigned long> & v);
100 
101  static const unsigned int VECTOR_STATE_SIZE = 20;
102 
103 private:
104 
105  static int numEngines;
106 
111 class Tausworthe {
112 public:
113 
114  Tausworthe();
115  Tausworthe(unsigned int seed);
116 
117  operator unsigned int();
118 
119  void put( std::ostream & os ) const;
120  void put(std::vector<unsigned long> & v) const;
121  void get( std::istream & is );
122  bool get(std::vector<unsigned long>::const_iterator & iv);
123 
124 private:
125 
126  int wordIndex;
127  unsigned int words[4];
128 }; // Tausworthe
129 
134 class IntegerCong {
135 public:
136 
137  IntegerCong();
138  IntegerCong(unsigned int seed, int streamNumber);
139 
140  operator unsigned int();
141 
142  void put( std::ostream & os ) const;
143  void put(std::vector<unsigned long> & v) const;
144  void get( std::istream & is );
145  bool get(std::vector<unsigned long>::const_iterator & iv);
146 
147 private:
148 
149  unsigned int state, multiplier, addend;
150 }; // IntegerCong
151 
152  Hurd288Engine & Hurd(); // retrieve the constituent engine for input
153 
154  const Tausworthe & ConstTaus() const; // Same as above
155  const IntegerCong & ConstCong() const; // necessary for
156  const Hurd288Engine & ConstHurd() const; // output
157 
158  Tausworthe tausworthe; // Instances of each of the
159  IntegerCong integerCong; // three engines that combine to make
160  Hurd288Engine hurd; // one TripleRand instance
161 
162 }; // TripleRand
163 
164 } // namespace CLHEP
165 
166 #ifdef ENABLE_BACKWARDS_COMPATIBILITY
167 // backwards compatibility will be enabled ONLY in CLHEP 1.9
168 using namespace CLHEP;
169 #endif
170 
171 #endif // TripleRand_h