| [3389] | 1 | #ifndef STSRAND_H_SEEN | 
|---|
|  | 2 | #define STSRAND_H_SEEN | 
|---|
|  | 3 |  | 
|---|
|  | 4 | // Classe RandomGenerator | 
|---|
|  | 5 | // Generateur aleatoire compatible multi-thread | 
|---|
|  | 6 | // | 
|---|
|  | 7 | // R. Ansari          (C) UPS+LAL IN2P3/CNRS  2007 | 
|---|
|  | 8 | // C. Magneville      (C) DAPNIA/SPP  CEA     2007 | 
|---|
|  | 9 |  | 
|---|
|  | 10 |  | 
|---|
|  | 11 | #include "machdefs.h" | 
|---|
|  | 12 | #include "objfio.h"   // Pour rendre la classe PPersist | 
|---|
|  | 13 |  | 
|---|
|  | 14 | #include "ndatablock.h" | 
|---|
|  | 15 |  | 
|---|
|  | 16 | namespace SOPHYA { | 
|---|
|  | 17 |  | 
|---|
|  | 18 | class RandomGenerator : public AnyDataObj { | 
|---|
|  | 19 |  | 
|---|
|  | 20 | public: | 
|---|
|  | 21 | RandomGenerator(size_t n = 1024, bool tsafe=true); | 
|---|
|  | 22 | RandomGenerator(RandomGenerator const & rg); | 
|---|
|  | 23 |  | 
|---|
|  | 24 | virtual ~RandomGenerator(); | 
|---|
|  | 25 |  | 
|---|
|  | 26 | /*! \brief Automatic initialization using the current time */ | 
|---|
|  | 27 | static  void AutoInit(int lp=0); | 
|---|
|  | 28 | /*! \brief Initialization using the specified seed  */ | 
|---|
|  | 29 | static  void Init(long seed_val, int lp=0); | 
|---|
|  | 30 | /*! \brief Initialization using the specified seed  */ | 
|---|
|  | 31 | static  void Init(unsigned short seed_16v[3], int lp=0); | 
|---|
|  | 32 | /*! \brief Getting the current seed state */ | 
|---|
|  | 33 | static  void GetSeed(unsigned short seed_16v[3], int lp=0); | 
|---|
|  | 34 |  | 
|---|
|  | 35 | /*! \brief Return a random number \b r with a flat distribution  0<=r<1 */ | 
|---|
|  | 36 | inline r_8 Flat01() {return Next();} | 
|---|
|  | 37 |  | 
|---|
|  | 38 | /*! \brief Return a random number \b r with a flat distribution  -1<=r<1 */ | 
|---|
|  | 39 | inline r_8 Flatpm1() {return 2.*Next()-1.;} | 
|---|
|  | 40 |  | 
|---|
|  | 41 | /*! \brief Return a random number following a gaussian distribution (sigma=1, mean=0*/ | 
|---|
|  | 42 | virtual r_8 Gaussian(); | 
|---|
|  | 43 |  | 
|---|
|  | 44 | /*! \brief Return a random number following a gaussian distribution "sigma", (mean=0)*/ | 
|---|
|  | 45 | virtual r_8 Gaussian(double sigma) | 
|---|
|  | 46 | { return sigma*Gaussian(); } | 
|---|
|  | 47 | /*! \brief Return a random number following a gaussian distribution with mean=mu, and sigma */ | 
|---|
|  | 48 | virtual r_8 Gaussian(double sigma,double mu) | 
|---|
|  | 49 | { return sigma*Gaussian()+mu; } | 
|---|
|  | 50 |  | 
|---|
|  | 51 | /*! \brief alias for Gaussian() */ | 
|---|
|  | 52 | inline r_8 NorRand() | 
|---|
|  | 53 | { return Gaussian(); } | 
|---|
|  | 54 |  | 
|---|
|  | 55 | /*! \brief Return a random number following a poisson distribution with mean mu */ | 
|---|
|  | 56 | virtual  uint_8 Poisson(double mu, double mumax); | 
|---|
|  | 57 |  | 
|---|
|  | 58 | /*! \brief Return a random number following a poisson distribution with mean mu */ | 
|---|
|  | 59 | inline uint_8 Poisson(double mu) | 
|---|
|  | 60 | { return Poisson(mu, -1.); } | 
|---|
|  | 61 |  | 
|---|
|  | 62 | /*! \brief Return a random number following a poisson distribution with mean mu */ | 
|---|
|  | 63 | inline uint_8  PoissRandLimit(double mu,double mumax=10.) | 
|---|
|  | 64 | { return Poisson(mu, mumax); } | 
|---|
|  | 65 |  | 
|---|
|  | 66 | //  Pour la gestion de persistance PPF | 
|---|
|  | 67 | friend class ObjFileIO<RandomGenerator> ; | 
|---|
|  | 68 |  | 
|---|
|  | 69 | protected: | 
|---|
|  | 70 | // ---- protected data members | 
|---|
|  | 71 | NDataBlock<r_8>  rseq_; | 
|---|
|  | 72 | size_t idx_; | 
|---|
|  | 73 | bool fg_nothrsafe;  // if true --> NOT thread-safe | 
|---|
|  | 74 | // ---- protected methods | 
|---|
|  | 75 | void GenSeq(); | 
|---|
|  | 76 | inline r_8 Next() | 
|---|
|  | 77 | { | 
|---|
|  | 78 | if (rseq_.Size() == 0)  return drand48(); | 
|---|
|  | 79 | else { | 
|---|
|  | 80 | if(idx_==rseq_.Size()) GenSeq(); | 
|---|
|  | 81 | return(rseq_(idx_++)); | 
|---|
|  | 82 | } | 
|---|
|  | 83 | } | 
|---|
|  | 84 | // Non thread-safe version of Init() and GetSeed() | 
|---|
|  | 85 | static  void Init_P(unsigned short seed_16v[3]); | 
|---|
|  | 86 | static  void GetSeed_P(unsigned short seed_16v[3]); | 
|---|
|  | 87 |  | 
|---|
|  | 88 | };  // Fin de la classe RandomGenerator | 
|---|
|  | 89 |  | 
|---|
|  | 90 | // Classe pour la gestion de persistance PPF :  ObjFileIO<RandomGenerator> | 
|---|
|  | 91 |  | 
|---|
|  | 92 | /*! Writes the random generator object state in the POutPersist stream \b os */ | 
|---|
|  | 93 | inline POutPersist& operator << (POutPersist& os, RandomGenerator & obj) | 
|---|
|  | 94 | { ObjFileIO<RandomGenerator> fio(&obj);  fio.Write(os);  return(os); } | 
|---|
|  | 95 | /*! Reads the random generator object state from the PInPersist stream \b is */ | 
|---|
|  | 96 | inline PInPersist& operator >> (PInPersist& is, RandomGenerator & obj) | 
|---|
|  | 97 | { ObjFileIO<RandomGenerator> fio(&obj); is.SkipToNextObject(); fio.Read(is); return(is); } | 
|---|
|  | 98 |  | 
|---|
|  | 99 | } /* namespace SOPHYA */ | 
|---|
|  | 100 |  | 
|---|
|  | 101 | #endif | 
|---|