CLHEP 2.0.4.7 Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

RanshiEngine.h

Go to the documentation of this file.
00001 // $Id: RanshiEngine.h,v 1.3.4.2 2005/04/15 16:32:53 garren Exp $
00002 // -*- C++ -*-
00003 //
00004 // -----------------------------------------------------------------------
00005 //                           HEP Random
00006 //                      --- RanshiEngine ---
00007 //                       class header file
00008 // -----------------------------------------------------------------------
00009 //
00010 //
00011 // The algorithm for this random engine was taken from "F.Gutbrod, Comp.
00012 // Phys. Comm. 87 (1995) 291-306".
00013 //
00014 // The algorithm can be imagined as a physical system as follows: Imagine
00015 // 512 "black balls" each with their own unique spin, and positions char- 
00016 // acterized by disrete angles, where the spin is a 32-bit unsigned integer.
00017 // A "red ball" collides based upon the angle determined by the last 8 bits
00018 // of its spin, and the spin of the colliding ball is taken as the output
00019 // random number. The spin of the colliding ball is replaced then with the
00020 // left circular shift of the black ball's spin XOR'd with the red ball's
00021 // spin. The black ball's old spin becomes the red ball's.
00022 //
00023 // To avoid the traps presented, two measures are taken: first, the red 
00024 // ball will oscillate between hitting the lower half of the buffer on one
00025 // turn and the upper half on another; second, the red ball's spin is 
00026 // incremented by a counter of the number of random numbers produced.
00027 //
00028 // The result is scaled to a double precision floating point number to which
00029 // is added another random double further scaled 2^(53-32) places to the
00030 // right in order to ensure that the remaining bits of the result are not 
00031 // left empty due to the mere 32 bits representation used internally.
00032 
00033 // =======================================================================
00034 // Ken Smith      - Created: 9th June 1998
00035 //                - Removed pow() from flat method: 21st Jul 1998
00036 //                - Added conversion operators:  6th Aug 1998
00037 // Mark Fischler    Methods put, get for instance save/restore 12/8/04    
00038 // Mark Fischler    methods for anonymous save/restore 12/27/04    
00039 // =======================================================================
00040 
00041 #ifndef HepRanshiEngine_h
00042 #define HepRanshiEngine_h
00043 
00044 #include "CLHEP/Random/defs.h"
00045 #include "CLHEP/Random/RandomEngine.h"
00046 
00047 namespace CLHEP {
00048 
00053 class RanshiEngine: public HepRandomEngine {
00054  
00055 public:
00056 
00057     RanshiEngine();
00058     RanshiEngine(std::istream &is);
00059     RanshiEngine(long seed);
00060     RanshiEngine(int rowIndex, int colIndex);
00061     virtual ~RanshiEngine();
00062     // Constructors and destructor
00063 
00064     RanshiEngine(const RanshiEngine &p);
00065     RanshiEngine & operator = (const RanshiEngine &p);
00066     // Copy constructor and operator=
00067 
00068     double flat();
00069     // Returns a pseudo random number between 0 and 1
00070 
00071     void flatArray(const int size, double* vect);
00072     // Fills the array "vect" of specified size with flat random values
00073 
00074     void setSeed(long seed, int);
00075     // Sets the state of the algorithm according to seed.
00076 
00077     void setSeeds(const long* seeds, int);
00078     // Sets the state of the algorithm according to the zero-terminated
00079     // array of seeds. 
00080 
00081     void saveStatus(const char filename[] = "RanshiEngine.conf") const;
00082     // Saves on named file the current engine status
00083 
00084     void restoreStatus(const char filename[] = "RanshiEngine.conf");
00085     // Reads from named file the last saved engine status
00086     // and restores it.
00087 
00088     void showStatus() const;
00089     // Dumps the engine status on the screen
00090 
00091     operator float();      // flat value, without worrying about filling bits
00092     operator unsigned int();  // 32-bit flat value, quickest of all
00093 
00094    virtual std::ostream & put (std::ostream & os) const;
00095    virtual std::istream & get (std::istream & is);
00096   static  std::string beginTag ( );
00097   virtual std::istream & getState ( std::istream & is );
00098 
00099    std::string name() const;
00100    static std::string engineName() {return "RanshiEngine";}
00101 
00102   std::vector<unsigned long> put () const;
00103   bool get (const std::vector<unsigned long> & v);
00104   bool getState (const std::vector<unsigned long> & v);
00105   
00106 private:
00107     static double twoToMinus_32;
00108     static double twoToMinus_53;
00109     static double nearlyTwoToMinus_54;
00110     void powersOfTwo();
00111 
00112     static int numEngines;
00113     enum {numBuff = 512};
00114 
00115     unsigned int halfBuff, numFlats;
00116     unsigned int buffer[numBuff];
00117     unsigned int redSpin;
00118 
00119     static const unsigned int VECTOR_STATE_SIZE = numBuff + 4;
00120   
00121 }; // RanshiEngine
00122 
00123 }  // namespace CLHEP
00124 
00125 #ifdef ENABLE_BACKWARDS_COMPATIBILITY
00126 //  backwards compatibility will be enabled ONLY in CLHEP 1.9
00127 using namespace CLHEP;
00128 #endif
00129 
00130 #endif // HepRanshiEngine_h

Generated on Thu Jul 1 22:02:30 2010 for CLHEP by  doxygen 1.4.7