Changeset 371
- Timestamp:
- Aug 5, 2005, 3:27:00 PM (18 years ago)
- Location:
- trunk/lib/random
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/random/Random.cc
r366 r371 2 2 3 3 #include <c++_tools/random/Random.h> 4 5 #include <c++_tools/statistics/Histogram.h> 6 4 7 5 8 namespace theplu { … … 35 38 36 39 40 DiscreteGeneral::DiscreteGeneral(const statistics::Histogram& hist) 41 { 42 double* p = new double[ hist.nof_bins() ]; 43 for (size_t i=0; i<hist.nof_bins(); i++) 44 p[ i ] = hist[i]; 45 gen_ = gsl_ran_discrete_preproc( hist.nof_bins(), p ); 46 delete p; 47 } 48 49 DiscreteGeneral::~DiscreteGeneral(void) 50 { 51 if( gen_ != 0 ) 52 gsl_ran_discrete_free( gen_ ); 53 gen_ = NULL; 54 } 55 56 37 57 }} // of namespace random and namespace theplu -
trunk/lib/random/Random.h
r369 r371 4 4 #define _theplu_random_ 5 5 6 #include <c++_tools/statistics/Histogram.h> 6 7 7 8 #include <gsl/gsl_rng.h> … … 171 172 }; 172 173 173 174 174 /// 175 175 /// @brief Discrete random distributions. … … 293 293 /// @brief General 294 294 /// 295 class DiscreteGeneral : public Discrete { 295 class DiscreteGeneral : public Discrete 296 { 297 public: 298 /// 299 /// @brief Constructor 300 /// 301 /// @param hist is a Histogram defining the probability distribution 302 /// 303 DiscreteGeneral(const statistics::Histogram& hist) ; 304 305 /// 306 /// @brief Destructor 307 /// 308 virtual ~DiscreteGeneral(void); 309 310 /// 311 /// The generated number is an integer and proportinal to the 312 /// frequency in the corresponding histogram bin. In other words, 313 /// the probability that 0 is returned is proportinal to the size 314 /// of the first bin. 315 /// 316 /// @return A random number. 317 /// 318 inline long operator()(void) const 319 { return gsl_ran_discrete(rng_->rng(), gen_); } 320 321 private: 322 gsl_ran_discrete_t* gen_; 323 }; 324 325 326 /// 327 /// Class to generate numbers from a histogram in a continuous manner. 328 /// 329 class ContinuousGeneral : public Continuous 330 { 331 public: 332 /// 333 /// @brief Constructor 334 /// 335 /// @param hist is a Histogram defining the probability distribution 336 /// 337 inline ContinuousGeneral(const statistics::Histogram& hist) 338 : discrete_(DiscreteGeneral(hist)), hist_(hist) {} 339 340 /// 341 /// @brief Destructor 342 /// 343 virtual ~ContinuousGeneral(void); 344 345 /// 346 /// The number is generated in a two step process. First the bin 347 /// in the histogram is randomly selected (see 348 /// DiscreteGeneral). Then a number is generated uniformly from 349 /// the interval defined by the bin. 350 /// 351 /// @return A random number. 352 /// 353 inline double operator()(void) const 354 { return hist_.observation_value(discrete_())+(u_()-0.5)*hist_.spacing(); } 355 356 private: 357 const DiscreteGeneral discrete_; 358 const statistics::Histogram& hist_; 359 ContinuousUniform u_; 296 360 }; 297 361
Note: See TracChangeset
for help on using the changeset viewer.