1 // $Id: Random.h,v 1.5 2010/06/16 17:24:53 garren Exp $ 2 // -*- C++ -*- 3 // 4 // ----------------------------------------------------------------------- 5 // HEP Random 6 // --- HepRandom --- 7 // class header file 8 // ----------------------------------------------------------------------- 9 // This file is part of Geant4 (simulation toolkit for HEP). 10 // 11 // It's a singleton instantiated by default within the HEP Random module. 12 // It uses an instantiated HepJamesRandom engine as default algorithm 13 // for pseudo-random number generation. HepRandom defines a static private 14 // data member theGenerator and a set of static inlined methods to manipulate 15 // it. By means of theGenerator the user can change the underlying engine 16 // algorithm, get and set the seeds and use any kind of defined random 17 // distribution. 18 // Distribution classes inherit from HepRandom and define both static and 19 // not-static interfaces. 20 // A static table of uncorrelated seeds is available in this class. 21 // A static method "getTheTableSeeds()" is defined to access a couple of 22 // seeds at a given index in the table. 23 24 // ======================================================================= 25 // Gabriele Cosmo - Created: 5th Sep 1995 26 // - Minor update: 17th May 1996 27 // - Poisson now operates on doubles : 31st Oct 1996 28 // - Added methods for engine status: 19th Nov 1996 29 // - Fixed default values to setTheSeed() and 30 // setTheSeeds() static methods: 16th Oct 1997 31 // - Modified HepRandom to act as a singleton, constructors 32 // are kept public for backward compatibility. Added table 33 // of seeds from HepRandomEngine: 19th Mar 1998 34 // - Relocated Poisson and Gauss data and simplified 35 // initialisation of static generator: 5th Jan 1999 36 // ======================================================================= 37 38 #ifndef HepRandom_h 39 #define HepRandom_h 1 40 41 #include "CLHEP/Random/defs.h" 42 #include "CLHEP/Random/RandomEngine.h" 43 44 namespace CLHEP { 45 46 /** 47 * @author <Gabriele.Cosmo@cern.ch> 48 * @ingroup random 49 */ 50 class HepRandom { 51 52 public: 53 54 HepRandom(); 55 HepRandom(long seed); 56 // Contructors with and without a seed using the default engine 57 // (MixMax). 58 59 HepRandom(HepRandomEngine & algorithm); 60 HepRandom(HepRandomEngine * algorithm); 61 // Constructor taking an alternative engine as argument. If a pointer is 62 // given the corresponding object will be deleted by the HepRandom 63 // destructor. 64 65 virtual ~HepRandom(); 66 // Destructor 67 68 // implicitly allow compiler-generated copy functions 69 70 double flat(); 71 // Returns the flat value ( interval ]0...1[ ). 72 73 void flatArray(const int size, double* vect); 74 // Fills "vect" array of flat random values, given the size. 75 76 inline double flat (HepRandomEngine* theNewEngine); 77 // Returns a flat value, given a defined Random Engine. 78 79 inline void flatArray(HepRandomEngine* theNewEngine, 80 const int size, double* vect); 81 // Fills "vect" array of flat random values, given the size 82 // and a defined Random Engine. 83 84 virtual double operator()(); 85 // To get a flat random number using the operator (). 86 87 virtual std::string name() const; 88 virtual HepRandomEngine & engine(); 89 90 91 virtual std::ostream & put ( std::ostream & os ) const; 92 virtual std::istream & get ( std::istream & is ); 93 // Save and restore to/from streams 94 95 // -------------------------------------------------- 96 // Static member functions using the static generator 97 // -------------------------------------------------- 98 99 static void setTheSeed(long seed, int lux=3); 100 // (Re)Initializes the generator with a seed. 101 102 static long getTheSeed(); 103 // Gets the current seed of the current generator. 104 105 static void setTheSeeds(const long* seeds, int aux=-1); 106 // (Re)Initializes the generator with a zero terminated list of seeds. 107 108 static const long* getTheSeeds(); 109 // Gets the current array of seeds of the current generator. 110 111 static void getTheTableSeeds (long* seeds, int index); 112 // Gets the array of seeds in the static seedTable at "index" position. 113 114 static HepRandom * getTheGenerator(); 115 // Return the current static generator. 116 117 static void setTheEngine (HepRandomEngine* theNewEngine); 118 // To set the underlying algorithm object. 119 120 static HepRandomEngine * getTheEngine(); 121 // Returns a pointer to the underlying algorithm object. 122 123 static void saveEngineStatus( const char filename[] = "Config.conf" ); 124 // Saves to file the current status of the current engine. 125 126 static void restoreEngineStatus( const char filename[] = "Config.conf" ); 127 // Restores a saved status (if any) for the current engine. 128 129 static std::ostream& saveFullState ( std::ostream & os ); 130 // Saves to stream the state of the engine and cached data. 131 132 static std::istream& restoreFullState ( std::istream & is ); 133 // Restores from stream the state of the engine and cached data. 134 saveDistState(std::ostream & os)135 static std::ostream& saveDistState ( std::ostream & os ) {return os;} 136 // Saves to stream the state of the cached data. 137 restoreDistState(std::istream & is)138 static std::istream& restoreDistState ( std::istream & is ) {return is;} 139 // Restores from stream the state of the cached data. 140 141 static std::ostream& saveStaticRandomStates ( std::ostream & os ); 142 // Saves to stream the engine and cached data for all distributions. 143 144 static std::istream& restoreStaticRandomStates ( std::istream & is ); 145 // Restores from stream the engine and cached data for all distributions. 146 147 static void showEngineStatus(); 148 // Dumps the current engine status on screen. 149 150 static int createInstance(); 151 // used to initialise the default engine 152 distributionName()153 static std::string distributionName() {return "HepRandomEngine";} 154 // Provides the name of this distribution class 155 156 protected: // -------- Data members --------- 157 158 static const long seedTable[215][2]; 159 // Table of seeds 160 161 }; 162 163 std::ostream & operator<< (std::ostream & os, const HepRandom & dist); 164 std::istream & operator>> (std::istream & is, HepRandom & dist); 165 166 } // namespace CLHEP 167 168 #ifdef ENABLE_BACKWARDS_COMPATIBILITY 169 // backwards compatibility will be enabled ONLY in CLHEP 1.9 170 using namespace CLHEP; 171 #endif 172 173 #include "CLHEP/Random/Random.icc" 174 175 #endif 176