Changeset 738 for trunk/yat/random
- Timestamp:
- Jan 8, 2007, 12:08:39 AM (17 years ago)
- Location:
- trunk/yat/random
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/yat/random/random.cc
r718 r738 24 24 #include "random.h" 25 25 #include "yat/statistics/Histogram.h" 26 #include "yat/utility/Exception.h" 26 27 27 28 #include <fstream> 28 #include < iostream>29 #include <sstream> 29 30 30 31 namespace theplu { … … 36 37 RNG::RNG(void) 37 38 { 38 gsl_rng_env_setup(); // support rng/seed changes through environment vars 39 rng_ = gsl_rng_alloc(gsl_rng_default); // Memory is allocated here! 39 // support rng/seed changes through environment vars 40 if (!gsl_rng_env_setup()) 41 throw utility::GSL_error("RNG: unknown generator"); 42 if (!(rng_=gsl_rng_alloc(gsl_rng_default))) 43 throw utility::GSL_error("RNG: failed to allocate memory"); 40 44 } 41 45 … … 105 109 RNG_state::RNG_state(const RNG* rng) 106 110 { 107 rng_ = gsl_rng_clone(rng->rng()); 111 if (!(rng_ = gsl_rng_clone(rng->rng()))) 112 throw utility::GSL_error("RNG_state: failed to allocate memory"); 108 113 } 109 114 … … 146 151 gen_ = gsl_ran_discrete_preproc( hist.nof_bins(), p ); 147 152 delete p; 153 if (!gen_) 154 throw utility::GSL_error("DiscreteGeneral: failed to setup generator."); 148 155 } 149 156 … … 163 170 164 171 165 DiscreteUniform::DiscreteUniform(void) 166 : range_(rng_->max()) 167 { 168 } 169 170 DiscreteUniform::DiscreteUniform(const u_long n) 172 DiscreteUniform::DiscreteUniform(u_long n) 171 173 : range_(n) 172 174 { 173 if (range_>rng_->max()) 174 range_=rng_->max(); 175 if (range_>rng_->max()) { 176 std::stringstream ss("DiscreteUniform: "); 177 ss << n << " is too large for RNG " << rng_->name(); 178 throw utility::GSL_error(ss.str()); 179 } 175 180 } 176 181 … … 178 183 u_long DiscreteUniform::operator()(void) const 179 184 { 180 return gsl_rng_uniform_int(rng_->rng(), range_); 181 } 182 183 184 u_long DiscreteUniform::operator()(const u_long n) const 185 { 186 return gsl_rng_uniform_int(rng_->rng(), n); 185 return (range_ ? 186 gsl_rng_uniform_int(rng_->rng(), range_) : gsl_rng_get(rng_->rng())); 187 } 188 189 190 u_long DiscreteUniform::operator()(u_long n) const 191 { 192 // making sure that n is not larger than the range of the 193 // underlying RNG 194 if (n>rng_->max()) { 195 std::stringstream ss("DiscreteUniform::op(): "); 196 ss << n << " is too large for RNG " << rng_->name(); 197 throw utility::GSL_error(ss.str()); 198 } 199 return gsl_rng_uniform_int(rng_->rng(),n); 187 200 } 188 201 -
trunk/yat/random/random.h
r718 r738 48 48 /// 49 49 /// There is information about how to change seeding and generators 50 /// at run time without recompilation using environment 51 /// variables. RNG of course support seeding at compile time if you 52 /// don't want to bother about environment variables and GSL. 50 /// at run time without recompilation using environment variables in 51 /// the GSL manual (Chapter on random number generators). RNG of 52 /// course support seeding at compile time if you don't want to 53 /// bother about environment variables and GSL. 53 54 /// 54 55 /// There are many different rng's available in GSL. Currently only … … 65 66 /// follow the coding style.) 66 67 /// 67 /// This implementation may be thread safe (according to GSL 68 /// documentation), but should be checked to be so before trusting 69 /// thread safety. 68 /// The current implementation is NOT thread safe since the RNG is 69 /// implemented as a singleton. However, the underlying GSL rng's 70 /// support thread safety since each instance of GSL rng's keep 71 /// track of their own state accordning to GSL documentation. 70 72 /// 71 73 /// @see Design Patterns (the singleton and adapter pattern). GSL … … 134 136 u_long seed_from_devurandom(void); 135 137 136 /// 137 /// @brief set the state 138 /// 139 /// @return see gsl_rng_memcpy 140 /// 138 /** 139 \brief Set the state to \a state. 140 141 \return 0 on success, non-zero otherwise. 142 143 \see gsl_rng_memcpy 144 */ 141 145 int set_state(const RNG_state&); 142 146 … … 276 280 { 277 281 public: 278 /// 279 /// @brief Default constructor. 280 /// 281 DiscreteUniform(void); 282 283 /// 284 /// @brief Constructor. 285 /// 286 /// The generator will generate integers from \f$ [0,n-1] \f$. If 287 /// \a n is larger than the maximum number the random number 288 /// generator can return, then (currently) \a n is adjusted 289 /// appropriately. 290 /// 291 /// @todo If a too large \a n is given an exception should be 292 /// thrown, i.e. the behaviour of this class will change. The case 293 /// when argument is 0 is not treated gracefully (underlying GSL 294 /// functionality will not return). 295 /// 296 DiscreteUniform(const u_long n); 297 298 /// 299 /// This function returns a random integer from 0 to n-1 300 /// inclusive. All integers in the range [0,n-1] are equally 301 /// likely. n is set in constructor. 302 /// 282 /** 283 \brief Constructor. 284 285 The generator will generate integers within the range \f$ 286 [0,n-1] \f$. If \a n is zero, then the whole range of the 287 underlying RNG will be used \f$ [min,max] \f$. Setting \a n to 288 zero is the preferred way to sample the whole range of the 289 underlying RNG, i.e. not setting \n to RNG.max. 290 291 \throw If \a n is larger than the maximum number the underlying 292 random number generator can return, then a GSL_error exception 293 is thrown. 294 */ 295 DiscreteUniform(u_long n=0); 296 297 /** 298 \brief Get a random number 299 300 The returned integer is either in the range [RNG.min,RNG.max] 301 or [0,n-1] depending on how the random number generator was 302 created. 303 304 \see DiscreteUniform(const u_long n=0) 305 */ 303 306 u_long operator()(void) const; 304 307 305 /// 306 /// This function returns a random integer from 0 to n-1 307 /// inclusive. All integers in the range [0,n-1] are equally 308 /// likely. 309 /// 310 u_long operator()(const u_long n) const; 308 /** 309 \brief Get a random integer in the range \f$ [0,n-1] \f$. 310 311 All integers in the range [0,n-1] are equally likely. This 312 function should be avoided for sampling the whole range of the 313 underlying RNG. 314 315 \throw GSL_error if \an is larger than the range of the 316 underlying generator. 317 */ 318 u_long operator()(u_long n) const; 311 319 312 320 private:
Note: See TracChangeset
for help on using the changeset viewer.