CLHEP/Random/DualRand.h

00001 // $Id: DualRand.h,v 1.9 2002/04/12 15:02:44 evc Exp $
00002 // -*- C++ -*-
00003 //
00004 // -----------------------------------------------------------------------
00005 //                           Hep Random
00006 //                        --- DualRand ---
00007 //                        class header file
00008 // -----------------------------------------------------------------------
00009 //                                                                      
00010 //  Canopy random number generator DualRand
00011 //      Re-written as C++ routine for 32-bit ints MF 1/26/98
00012 //
00013 //  Exclusive or of a feedback shift register and integer congruence
00014 //  random number generator.  The feedback shift register uses offsets
00015 //  127 and 97.  The integer congruence generator uses a different
00016 //  multiplier for each stream.  The multipliers are chosen to give
00017 //  full period and maximum "potency" for modulo 2^32.  The period of
00018 //  the combined random number generator is 2^159 - 2^32, and the
00019 //  sequences are different for each stream (not just started in a
00020 //  different place).
00021 //
00022 // =======================================================================
00023 //  Canopy random number generator DualRand.
00024 //      Doug Toussaint   5/25/88
00025 //      Optimized by GMH 7/26/88
00026 //      Optimized by GMH 7/26/88
00027 //      Repaired  by GMH 12/1/88 to update modular congruence state
00028 //      Put into ranlib by GMH 6/23/89
00029 //      Re-written as C++ routine for 32-bit ints MF 1/26/98
00030 //      Re-written for CLHEP package             KLS 6/04/98
00031 //      Removed pow() from flat method for speed KLS 7/21/98
00032 // Ken Smith      - Added conversion operators:  6th Aug 1998
00033 // =======================================================================
00034 
00035 
00036 #ifndef DualRand_h
00037 #define DualRand_h
00038 
00039 #include "CLHEP/Random/RandomEngine.h"
00040 
00045 class DualRand: public HepRandomEngine {
00046 
00047 public:
00048 
00049   DualRand();
00050   DualRand(long seed);
00051   DualRand(HepStd::istream & is);
00052   DualRand(int rowIndex, int colIndex);
00053   virtual ~DualRand();
00054 
00055   DualRand(const DualRand & p);
00056   DualRand & operator=(const DualRand & p);
00057 
00058   double flat();
00059   // Returns a pseudo random number between 0 and 1 
00060   // (excluding the end points)
00061 
00062   void flatArray(const int size, double * vect);
00063   // Fills an array "vect" of specified size with flat random values.
00064 
00065   void setSeed(long seed, int);
00066   // Sets the state of the algorithm according to seed.
00067 
00068   void setSeeds(const long * seeds, int);
00069   // Sets the state of the algorithm according to the zero-terminated 
00070   // array of seeds.
00071 
00072   void saveStatus( const char filename[] = "DualRand.conf") const;
00073   // Saves on named file the current engine status.
00074 
00075   void restoreStatus( const char filename[] = "DualRand.conf" );
00076   // Reads from named file the last saved engine status and restores it.
00077 
00078   void showStatus() const;
00079   // Dumps the current engine status on the screen.
00080 
00081   operator float();      // flat value, without worrying about filling bits
00082   operator unsigned int();  // 32-bit flat value, quickest of all
00083 
00084   friend HepStd::ostream & operator<< (HepStd::ostream & os, const DualRand & e);
00085   friend HepStd::istream & operator>> (HepStd::istream & is,       DualRand & e);
00086 
00087 private:
00088 
00089   static int numEngines;
00090 
00091   static double twoToMinus_32;
00092   static double twoToMinus_53;
00093   static double nearlyTwoToMinus_54;
00094   void powersOfTwo();
00095 
00096   // This generator is composed of two others combined:
00097 
00098   class Tausworthe {
00099   public:
00100     Tausworthe();
00101     Tausworthe(unsigned int seed);
00102     operator unsigned int();
00103     void put(HepStd::ostream & os) const;
00104     void get(HepStd::istream & is);
00105   private:
00106     int wordIndex;
00107     unsigned int words[4];
00108   }; // Tausworthe
00109 
00110   class IntegerCong {
00111   public:
00112     IntegerCong();
00113     IntegerCong(unsigned int seed, int streamNumber);
00114     operator unsigned int();
00115     void put(HepStd::ostream & os) const;
00116     void get(HepStd::istream & is);
00117   private:
00118     unsigned int state, multiplier, addend;
00119   }; // IntegerCong
00120 
00121   Tausworthe  tausworthe;
00122   IntegerCong integerCong;
00123 
00124 }; // DualRand
00125 
00126 #endif // DualRand_h

Class Library for High Energy Physics (version 1.8)