Changeset 379
- Timestamp:
- Aug 8, 2005, 4:39:24 PM (17 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/random/random.cc
r375 r379 60 60 61 61 62 DiscreteUniform::DiscreteUniform(void) 63 : range_(rng_->max()+1) 64 { 65 } 66 67 68 69 DiscreteUniform::DiscreteUniform(const u_long range) 70 : range_(range) 71 { 72 73 if ( range_-1 > rng_->max()){ 74 range_ = rng_->max()+1; 75 } 76 77 78 79 } 80 81 82 62 83 DiscreteGeneral::DiscreteGeneral(const statistics::Histogram& hist) 63 84 { … … 79 100 80 101 102 81 103 }} // of namespace random and namespace theplu -
trunk/lib/random/random.h
r378 r379 27 27 /// There are many different rng's available in GSL. Currently only 28 28 /// the default generator is implemented and no other one is 29 /// choosable th ough the class interface. This means that you have29 /// choosable through the class interface. This means that you have 30 30 /// to fall back to the use of environment variables as described in 31 31 /// the GSL documentation, or be bold and request support for other … … 251 251 /// @return A random number. 252 252 /// 253 virtual long operator()(void) const = 0;253 virtual u_long operator()(void) const = 0; 254 254 255 255 protected: … … 262 262 /// Discrete uniform distribution also known as the "equally likely 263 263 /// outcomes" distribution. Each outcome, in this case an integer 264 /// from \f$ [a,b) \f$, have equal probability to occur.264 /// from [a,b) , have equal probability to occur. 265 265 /// 266 266 /// Distribution function \f$ p(k) = \frac{1}{b-a} \f$ for \f$ a \le 267 267 /// k < b \f$ \n 268 /// Expectation value: \f$ \frac{a+b }{2} \f$ \n268 /// Expectation value: \f$ \frac{a+b-1}{2} \f$ \n 269 269 /// Variance: \f$ \frac{1}{3}((b-a)^2-1) \f$ 270 270 /// … … 276 276 /// @brief Constructor. 277 277 /// 278 /// @todo The default behaviour of the underlying RNG should be 279 /// setup when this constructor is used. 280 /// 281 inline DiscreteUniform(void) 282 : min_(0), range_(0) {} 278 DiscreteUniform(void); 283 279 284 280 /// 285 281 /// @brief Constructor. 286 282 /// 287 /// @param \a range is number of different integers that can be 288 /// generated \f$ (b-a+1) \f$ \a min is the minimal integer that 289 /// can be generated \f$ (a) \f$ 290 /// 291 inline DiscreteUniform(const u_long range, const long min=0) 292 : min_(min), range_(range) {} 293 294 /// 295 /// @return A random number between \a min_ and \a range_ - \a min_ 296 /// 297 inline long operator()(void) const 298 { return gsl_rng_uniform_int(rng_->rng(), range_)+min_; } 299 300 /// 301 /// @return A random number between 0 and \a range. 302 /// 303 /// @note This operator ignores any set minimum range parameter. 304 /// 305 inline long operator()(const u_long range) const 306 { return gsl_rng_uniform_int(rng_->rng(), range); } 307 308 /// 309 /// @return A random number between \a min and \a range - \a min 310 /// 311 inline long operator()(const u_long range, const long min) const 312 { return gsl_rng_uniform_int(rng_->rng(), range)+min; } 313 314 315 private: 316 long min_; 283 /// @param n sets the range. Object will generate integers from 284 /// [0,n-1]. 285 /// 286 /// @todo exception should be thrown, when n is out of range 287 /// 288 DiscreteUniform(const u_long n); 289 290 /// 291 /// This function returns a random integer from 0 to n-1 292 /// inclusive. All integers in the range [0,n-1] are equally 293 /// likely. n is set in constructor. 294 /// 295 inline u_long operator()(void) const 296 { return gsl_rng_uniform_int(rng_->rng(), range_); } 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. 302 /// 303 /// @todo exception should be thrown, when n is out of range 304 /// 305 inline u_long operator()(const u_long n) const 306 { return gsl_rng_uniform_int(rng_->rng(), n); } 307 308 309 private: 317 310 u_long range_; 318 311 }; … … 347 340 /// @return A Poisson distributed number. 348 341 /// 349 inline long operator()(void) const342 inline u_long operator()(void) const 350 343 { return gsl_ran_poisson(rng_->rng(), m_); } 351 344 … … 387 380 /// @return A random number. 388 381 /// 389 inline long operator()(void) const382 inline u_long operator()(void) const 390 383 { return gsl_ran_discrete(rng_->rng(), gen_); } 391 384 -
trunk/test/rnd_test.cc
r377 r379 4 4 5 5 #include <iostream> 6 6 #include <sstream> 7 #include <fstream> 7 8 8 9 int main(const int argc,const char* argv[]) 9 10 { 11 std::ostream* error; 12 if (argc>1 && argv[1]==std::string("-p")) 13 error = &std::cerr; 14 else 15 error = new std::ofstream("/dev/null"); 16 17 bool ok = true; 18 10 19 theplu::random::ContinuousUniform uniform; 11 20 12 21 theplu::random::RNG* rng=theplu::random::RNG::instance(); 13 22 23 // testing that minimal integer is zero for our generator 24 if (rng->min()){ 25 *error << "Error: rng->min is not zero" << std::endl; 26 ok = false; 27 } 28 29 if (error!=&std::cerr) 30 delete error; 31 32 if (!ok) 33 return -1; 14 34 return 0; 15 35 }
Note: See TracChangeset
for help on using the changeset viewer.