Changeset 374
- Timestamp:
- Aug 6, 2005, 12:30:57 AM (17 years ago)
- Location:
- trunk/lib/random
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/random/Random.cc
r371 r374 2 2 3 3 #include <c++_tools/random/Random.h> 4 #include <c++_tools/statistics/Histogram.h> 4 5 5 #include <c++_tools/statistics/Histogram.h> 6 #include <fstream> 7 #include <iostream> 6 8 7 9 … … 13 15 14 16 15 RNG::RNG( void)17 RNG::RNG(u_long s) 16 18 { 17 gsl_rng_env_setup(); 19 gsl_rng_env_setup(); // support rng/seed changes through environment vars 18 20 rng_ = gsl_rng_alloc(gsl_rng_default); // Memory is allocated here! 21 seed(s); 19 22 } 20 23 … … 30 33 31 34 32 RNG* RNG::instance( intseed)35 RNG* RNG::instance(u_long seed) 33 36 { 34 37 if (!instance_) 35 instance_ = new RNG( );38 instance_ = new RNG(seed); 36 39 return instance_; 37 40 } 41 42 43 44 u_long RNG::seed_from_devurandom(void) 45 { 46 u_char ulongsize=sizeof(u_long); 47 char* buffer=new char[ulongsize]; 48 std::ifstream is("/dev/urandom"); 49 is.read(buffer,ulongsize); 50 is.close(); 51 u_long s=0; 52 for (u_int i=0; i<ulongsize; i++) { 53 u_char ch=buffer[i]; 54 s+=ch<<((ulongsize-1-i)*8); 55 } 56 seed(s); 57 return s; 58 } 59 38 60 39 61 … … 47 69 } 48 70 71 72 49 73 DiscreteGeneral::~DiscreteGeneral(void) 50 74 { 51 if ( gen_ != 0 )75 if (gen_) 52 76 gsl_ran_discrete_free( gen_ ); 53 77 gen_ = NULL; -
trunk/lib/random/Random.h
r371 r374 9 9 #include <gsl/gsl_randist.h> 10 10 11 #include <string> 12 11 13 namespace theplu { 12 14 namespace random { 13 15 14 16 /// 15 /// The RNG class provides a single global random number generator 16 /// instance with one point of access to the generator. 17 /// 18 /// This is probably not thread safe. 19 /// 20 /// @see Design Patterns (the singleton and adapter pattern) 17 /// The RNG class is wrapper to the GSL random number generator 18 /// (rng). This class provides a single global instance of the rng, 19 /// and makes sure there is only one point of access to the 20 /// generator. 21 /// 22 /// There is information about how to change seeding and generators 23 /// at run time without recompilation using environment 24 /// variables. RNG of course support seeding at compile time if you 25 /// don't want to bother about environment variables and GSL. 26 /// 27 /// There are many different rng's available in GSL. Currently only 28 /// the default generator is implemented and no other one is 29 /// choosable though the class interface. This means that you have 30 /// to fall back to the use of environment variables as described in 31 /// the GSL documentation, or be bold and request support for other 32 /// rng's through the class interface. 33 /// 34 /// Not all GSL functionality is implemented, we'll add 35 /// functionality when needed and may do it when requested. Better 36 /// yet, supply us with code and we will probably add it to the code 37 /// (BUT remember to implement reasonable tests for your code and 38 /// follow the coding style.) 39 /// 40 /// This implementation may be thread safe (according to GSL 41 /// documentation), but should be checked to be so before trusting 42 /// thread safety. 43 /// 44 /// @see Design Patterns (the singleton and adapter pattern). GSL 45 /// documentation. 21 46 /// 22 47 class RNG … … 26 51 virtual ~RNG(void); 27 52 28 static RNG* instance(int seed); 53 static RNG* instance(u_long seed=0); 54 55 /// 56 /// @brief Returns the largest number that the random number 57 /// generator can return. 58 /// 59 inline u_long max(void) const { return gsl_rng_max(rng_); } 60 61 /// 62 /// @brief Returns the smallest number that the random number 63 /// generator can return. 64 /// 65 inline u_long min(void) const { return gsl_rng_min(rng_); } 66 67 /// 68 /// @brief Returns the name of the random number generator 69 /// 70 inline std::string name(void) const { return gsl_rng_name(rng_); } 29 71 30 72 inline const gsl_rng* rng(void) const { return rng_; } 31 73 32 private: 33 34 RNG(void); 74 /// 75 /// Set the seed \a s for the rng. If \a s is zero, a default 76 /// value from the rng's original implementation is used (cf. GSL 77 /// documentation). 78 /// 79 /// @brief Set the seed \a s for the rng. 80 /// 81 /// @see seed_dev_urandom 82 /// 83 inline void seed(u_long s) const { gsl_rng_set(rng_,s); } 84 85 /// 86 /// @brief Seed the rng using the /dev/urandom device. 87 /// 88 /// @return The seed acquired from /dev/urandom. 89 /// 90 u_long seed_from_devurandom(void); 91 92 private: 93 94 RNG(u_long seed); 35 95 36 96 static RNG* instance_; … … 41 101 42 102 /// 43 /// @brief Continuous random distributions.44 /// 45 /// Abstract base class for continuous random distributions.103 /// @brief Continuous random number distributions. 104 /// 105 /// Abstract base class for continuous random number distributions. 46 106 /// 47 107 class Continuous … … 52 112 /// @brief Constructor 53 113 /// 54 inline Continuous(void) { rng_=RNG::instance( 89); }114 inline Continuous(void) { rng_=RNG::instance(); } 55 115 56 116 /// … … 68 128 /// 69 129 /// Class for generating a random number from a uniform distribution 70 /// between zero and unity.130 /// in the range [0,1), i.e. zero is included but not 1. 71 131 /// 72 132 /// Distribution function \f$ f(x) = 1 \f$ for \f$ 0 \le x < 1 \f$ \n … … 173 233 174 234 /// 175 /// @brief Discrete random distributions. 176 /// 177 /// Abstract Base Class for discrete random distributions. Given K 178 /// discrete events with different probabilities \f$ P[k] \f$, 179 /// produce a random value k consistent with its probability. 235 /// @brief Discrete random number distributions. 236 /// 237 /// Abstract base class for discrete random number 238 /// distributions. Given K discrete events with different 239 /// probabilities \f$ P[k] \f$, produce a random value k consistent 240 /// with its probability. 180 241 /// 181 242 class Discrete … … 185 246 /// @brief Constructor 186 247 /// 187 inline Discrete(void) { rng_=RNG::instance( 89); }248 inline Discrete(void) { rng_=RNG::instance(); } 188 249 189 250 ///
Note: See TracChangeset
for help on using the changeset viewer.