1 // $Id: MTwistEngine.h,v 1.5 2010/06/16 17:24:53 garren Exp $
2 // -*- C++ -*-
3 //
4 // -----------------------------------------------------------------------
5 //                             HEP Random
6 //                        --- MTwistEngine ---
7 //                          class header file
8 // -----------------------------------------------------------------------
9 // A "fast, compact, huge-period generator" based on M. Matsumoto and
10 // T. Nishimura, "Mersenne Twister: A 623-dimensionally equidistributed
11 // uniform pseudorandom number generator", to appear in ACM Trans. on
12 // Modeling and Computer Simulation.  It is a twisted GFSR generator
13 // with a Mersenne-prime period of 2^19937-1, uniform on open interval (0,1)
14 // For further information, see http://www.math.keio.ac.jp/~matumoto/emt.html
15 // =======================================================================
16 // Ken Smith      - Started initial draft: 14th Jul 1998
17 //                - Optimized to get pow() out of flat() method: 21st Jul
18 //                - Added conversion operators:  6th Aug 1998
19 // M Fischler	  - Changes in way powers of two are kept: 16-Sep-1998
20 // Mark Fischler  - Methods for distrib. instance save/restore 12/8/04
21 // Mark Fischler    methods for anonymous save/restore 12/27/04
22 // =======================================================================
23 
24 #ifndef MTwistEngine_h
25 #define MTwistEngine_h
26 
27 #include "CLHEP/Random/defs.h"
28 #include "CLHEP/Random/RandomEngine.h"
29 
30 namespace CLHEP {
31 
32 /**
33  * @author
34  * @ingroup random
35  */
36 class MTwistEngine : public HepRandomEngine {
37 
38 public:
39 
40   MTwistEngine();
41   MTwistEngine( long seed );
42   MTwistEngine( int rowIndex, int colIndex );
43   MTwistEngine( std::istream & is );
44   virtual ~MTwistEngine();
45   // Constructors and destructor.
46 
47   double flat();
48   // Returns a pseudo random number between 0 and 1 (excluding the end points).
49 
50   void flatArray(const int size, double* vect);
51   // Fills an array "vect" of specified size with flat random values.
52 
53   void setSeed(long seed, int);
54   // Sets the state of the algorithm according to seed.
55 
56   void setSeeds(const long * seeds, int);
57   // Sets the state of the algorithm according to the zero terminated
58   // array of seeds. It is allowed to ignore one or many seeds in this array.
59 
60   void saveStatus( const char filename[] = "MTwist.conf") const;
61   // Saves the current engine status in the named file
62 
63   void restoreStatus( const char filename[] = "MTwist.conf" );
64   // Reads from named file the the last saved engine status and restores it.
65 
66   void showStatus() const;
67   // Dumps the current engine status on the screen.
68 
69   operator double();       // Returns same as flat()
70   operator float();        // returns flat, without worrying about filling bits
71   operator unsigned int(); // 32-bit flat, quickest of all
72 
73   virtual std::ostream & put (std::ostream & os) const;
74   virtual std::istream & get (std::istream & is);
75   static  std::string beginTag ( );
76   virtual std::istream & getState ( std::istream & is );
77 
78   std::string name() const;
engineName()79   static std::string engineName() {return "MTwistEngine";}
80 
81   std::vector<unsigned long> put () const;
82   bool get (const std::vector<unsigned long> & v);
83   bool getState (const std::vector<unsigned long> & v);
84 
85   static const unsigned int VECTOR_STATE_SIZE = 626;
86 
87 private:
88 
89   unsigned int mt[624];
90   int count624;
91 
92   enum{ NminusM = 227, M = 397, N = 624};
93 
94 }; // MTwistEngine
95 
96 }  // namespace CLHEP
97 
98 #ifdef ENABLE_BACKWARDS_COMPATIBILITY
99 //  backwards compatibility will be enabled ONLY in CLHEP 1.9
100 using namespace CLHEP;
101 #endif
102 
103 #endif // MTwistEngine_h
104