Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:37

0001 // $Id: DualRand.h,v 1.5 2010/06/16 17:24:53 garren Exp $
0002 // -*- C++ -*-
0003 //
0004 // -----------------------------------------------------------------------
0005 //                           Hep Random
0006 //                        --- DualRand ---
0007 //                        class header file
0008 // -----------------------------------------------------------------------
0009 //                                  
0010 //  Canopy random number generator DualRand
0011 //  Re-written as C++ routine for 32-bit ints MF 1/26/98
0012 //
0013 //  Exclusive or of a feedback shift register and integer congruence
0014 //  random number generator.  The feedback shift register uses offsets
0015 //  127 and 97.  The integer congruence generator uses a different
0016 //  multiplier for each stream.  The multipliers are chosen to give
0017 //  full period and maximum "potency" for modulo 2^32.  The period of
0018 //  the combined random number generator is 2^159 - 2^32, and the
0019 //  sequences are different for each stream (not just started in a
0020 //  different place).
0021 //
0022 // =======================================================================
0023 //  Canopy random number generator DualRand.
0024 //  Doug Toussaint   5/25/88                               
0025 //  Optimized by GMH 7/26/88                               
0026 //  Optimized by GMH 7/26/88                               
0027 //  Repaired  by GMH 12/1/88 to update modular congruence state            
0028 //  Put into ranlib by GMH 6/23/89                         
0029 //  Re-written as C++ routine for 32-bit ints MF 1/26/98               
0030 //  Re-written for CLHEP package         KLS 6/04/98               
0031 //  Removed pow() from flat method for speed KLS 7/21/98               
0032 //  Ken Smith      - Added conversion operators:  6th Aug 1998             
0033 //  Mark Fischler    methods for distrib. instance save/restore 12/8/04    
0034 //  Mark Fischler    methods for anonymous save/restore 12/27/04    
0035 // Mark Fischler  - methods for vector save/restore 3/7/05    
0036 // =======================================================================
0037 
0038 
0039 #ifndef DualRand_h
0040 #define DualRand_h
0041 
0042 #include "CLHEP/Random/defs.h"
0043 #include "CLHEP/Random/RandomEngine.h"
0044 
0045 namespace CLHEP {
0046 
0047 /**
0048  * @author
0049  * @ingroup random
0050  */
0051 class DualRand: public HepRandomEngine {
0052 
0053 public:
0054 
0055   DualRand();
0056   DualRand(long seed);
0057   DualRand(std::istream & is);
0058   DualRand(int rowIndex, int colIndex);
0059   virtual ~DualRand();
0060 
0061   // let the compiler generate the copy constructors
0062   //DualRand(const DualRand & p);
0063   //DualRand & operator=(const DualRand & p);
0064 
0065   double flat();
0066   // Returns a pseudo random number between 0 and 1 
0067   // (excluding the end points)
0068 
0069   void flatArray(const int size, double * vect);
0070   // Fills an array "vect" of specified size with flat random values.
0071 
0072   void setSeed(long seed, int);
0073   // Sets the state of the algorithm according to seed.
0074 
0075   void setSeeds(const long * seeds, int);
0076   // Sets the state of the algorithm according to the zero-terminated 
0077   // array of seeds.
0078 
0079   void saveStatus( const char filename[] = "DualRand.conf") const;
0080   // Saves on named file the current engine status.
0081 
0082   void restoreStatus( const char filename[] = "DualRand.conf" );
0083   // Reads from named file the last saved engine status and restores it.
0084 
0085   void showStatus() const;
0086   // Dumps the current engine status on the screen.
0087 
0088   operator double();        // Returns same as flat()
0089   operator float();         // flat value, without worrying about filling bits
0090   operator unsigned int();  // 32-bit flat value, quickest of all
0091 
0092   virtual std::ostream & put (std::ostream & os) const;
0093   virtual std::istream & get (std::istream & is);
0094   static  std::string beginTag ( );
0095   virtual std::istream & getState ( std::istream & is );
0096     
0097   std::string name() const;
0098   static std::string engineName() {return "DualRand";}
0099 
0100   std::vector<unsigned long> put () const;
0101   bool get (const std::vector<unsigned long> & v);
0102   bool getState (const std::vector<unsigned long> & v);
0103   
0104   static const unsigned int VECTOR_STATE_SIZE = 9;
0105 
0106 private:
0107 
0108   // This generator is composed of two others combined:
0109 
0110   class Tausworthe {
0111   public:
0112     Tausworthe();
0113     Tausworthe(unsigned int seed);
0114     operator unsigned int();
0115     void put(std::ostream & os) const;
0116     void put(std::vector<unsigned long> & v) const;
0117     void get(std::istream & is);
0118     bool get(std::vector<unsigned long>::const_iterator & iv);
0119   private:
0120     int wordIndex;
0121     unsigned int words[4];
0122   }; // Tausworthe
0123 
0124   class IntegerCong {
0125   public:
0126     IntegerCong();
0127     IntegerCong(unsigned int seed, int streamNumber);
0128     operator unsigned int();
0129     void put(std::ostream & os) const;
0130     void put(std::vector<unsigned long> & v) const;
0131     void get(std::istream & is);
0132     bool get(std::vector<unsigned long>::const_iterator & iv);
0133   private:
0134     unsigned int state, multiplier, addend;
0135   }; // IntegerCong
0136 
0137   int numEngines;
0138   Tausworthe  tausworthe;
0139   IntegerCong integerCong;
0140 
0141 }; // DualRand
0142 
0143 }  // namespace CLHEP
0144 
0145 #ifdef ENABLE_BACKWARDS_COMPATIBILITY
0146 //  backwards compatibility will be enabled ONLY in CLHEP 1.9
0147 using namespace CLHEP;
0148 #endif
0149 
0150 #endif // DualRand_h