CLHEP/Random/MTwistEngine.h

00001 // $Id: MTwistEngine.h,v 1.9 2002/04/12 15:02:44 evc Exp $
00002 // -*- C++ -*-
00003 //
00004 // -----------------------------------------------------------------------
00005 //                             HEP Random
00006 //                        --- MTwistEngine ---
00007 //                          class header file
00008 // -----------------------------------------------------------------------
00009 // A "fast, compact, huge-period generator" based on M. Matsumoto and 
00010 // T. Nishimura, "Mersenne Twister: A 623-dimensionally equidistributed 
00011 // uniform pseudorandom number generator", to appear in ACM Trans. on
00012 // Modeling and Computer Simulation.  It is a twisted GFSR generator
00013 // with a Mersenne-prime period of 2^19937-1, uniform on open interval (0,1)
00014 // For further information, see http://www.math.keio.ac.jp/~matumoto/emt.html
00015 // =======================================================================
00016 // Ken Smith      - Started initial draft: 14th Jul 1998
00017 //                - Optimized to get pow() out of flat() method: 21st Jul
00018 //                - Added conversion operators:  6th Aug 1998
00019 // M Fischler     - Changes in way powers of two are kept: 16-Sep-1998
00020 // =======================================================================
00021 
00022 #ifndef MTwistEngine_h
00023 #define MTwistEngine_h
00024 
00025 #include "CLHEP/Random/RandomEngine.h"
00026 
00031 class MTwistEngine : public HepRandomEngine {
00032 
00033 public:
00034 
00035   MTwistEngine();
00036   MTwistEngine( long seed );
00037   MTwistEngine( int rowIndex, int colIndex );
00038   MTwistEngine( HepStd::istream & is );
00039   virtual ~MTwistEngine();
00040   // Constructors and destructor.
00041 
00042   MTwistEngine( const MTwistEngine & p );
00043   MTwistEngine & operator=( const MTwistEngine & p );
00044   // Copy constructor and operator=.
00045 
00046   double flat();
00047   // Returns a pseudo random number between 0 and 1 (excluding the end points).
00048 
00049   void flatArray(const int size, double* vect);
00050   // Fills an array "vect" of specified size with flat random values.
00051 
00052   void setSeed(long seed, int);
00053   // Sets the state of the algorithm according to seed.
00054 
00055   void setSeeds(const long * seeds, int);
00056   // Sets the state of the algorithm according to the zero terminated
00057   // array of seeds. It is allowed to ignore one or many seeds in this array.
00058 
00059   void saveStatus( const char filename[] = "MTwist.conf") const;
00060   // Saves the current engine status in the named file
00061 
00062   void restoreStatus( const char filename[] = "MTwist.conf" );
00063   // Reads from named file the the last saved engine status and restores it.
00064 
00065   void showStatus() const;
00066   // Dumps the current engine status on the screen.
00067 
00068   operator float();     // returns flat, without worrying about filling bits
00069   operator unsigned int(); // 32-bit flat, quickest of all
00070 
00071   friend HepStd::ostream & operator<< (HepStd::ostream & os, const MTwistEngine & e);
00072   friend HepStd::istream & operator>> (HepStd::istream & is,       MTwistEngine & e);
00073 
00074 private:
00075 
00076   unsigned int mt[624];
00077   int count624;
00078 
00079   static double twoToMinus_32;
00080   static double twoToMinus_53;
00081   static double nearlyTwoToMinus_54;
00082   void powersOfTwo();
00083 
00084   enum{ NminusM = 227, M = 397, N = 624};
00085   static int numEngines;
00086   static int maxIndex;
00087 
00088 }; // MTwistEngine
00089 
00090 #endif // MTwistEngine_h

Class Library for High Energy Physics (version 1.8)