yat  0.14.5pre
random.h
1 #ifndef _theplu_yat_random_
2 #define _theplu_yat_random_
3 
4 // $Id: random.h 3570 2017-01-09 03:46:22Z peter $
5 
6 /*
7  Copyright (C) 2005, 2006, 2007, 2008 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 Peter Johansson
9 
10  This file is part of the yat library, http://dev.thep.lu.se/yat
11 
12  The yat library is free software; you can redistribute it and/or
13  modify it under the terms of the GNU General Public License as
14  published by the Free Software Foundation; either version 3 of the
15  License, or (at your option) any later version.
16 
17  The yat library is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with yat. If not, see <http://www.gnu.org/licenses/>.
24 */
25 
26 #include "yat/statistics/Histogram.h"
27 #include "yat/utility/deprecate.h"
28 
29 // always include this before <boost/excpetion_ptr.hpp> indirectly below
30 #include "yat/utility/boost_exception_ptr.h"
31 
32 #include <boost/concept_check.hpp>
33 #include <boost/iterator/iterator_concepts.hpp>
34 #include <boost/thread.hpp>
35 #include <boost/thread/tss.hpp>
36 
37 #include <gsl/gsl_rng.h>
38 #include <gsl/gsl_randist.h>
39 
40 #include <algorithm>
41 #include <string>
42 #include <vector>
43 
44 namespace theplu {
45 namespace yat {
46 namespace random {
47 
48  //forward declaration
49  class RNG_state;
50 
86  class RNG
87  {
88  public:
89 
107  static RNG* instance(void);
108 
113  unsigned long max(void) const;
114 
119  unsigned long min(void) const;
120 
124  std::string name(void) const;
125 
133  const gsl_rng* rng(void) const;
134 
148  void seed(unsigned long s) const;
149 
158  unsigned long seed_from_devurandom(void);
159 
171  // return int for backward compatibility with yat 0.8
172  int set_state(const RNG_state&);
173 
174  private:
175  RNG(void);
176 
184  RNG(const RNG&);
185 
190  RNG& operator=(const RNG&);
191 
192  virtual ~RNG(void);
193  void rng_alloc(void) const;
194 
195  static RNG* instance_;
196  // holds one gsl_rng per thread. Access through rng(void) so a
197  // gsl_rng is allocated if necessary.
198  mutable boost::thread_specific_ptr<gsl_rng> rng_;
199  mutable unsigned long seed_;
200  // guard needs to be mutable because major mission for it is to protect seed_ against multi-access, and seed_ is mutable...
201  mutable boost::mutex mutex_;
202  static boost::mutex init_mutex_;
203  };
204 
205 
209  class RNG_state
210  {
211  public:
215  explicit RNG_state(const RNG*);
216 
222  RNG_state(const RNG_state&);
223 
227  ~RNG_state(void);
228 
232  const gsl_rng* rng(void) const;
233 
239  RNG_state& operator=(const RNG_state&);
240 
241  private:
242  gsl_rng* rng_;
243 
244  void clone(const gsl_rng&);
245  };
246 
247 
248  // --------------------- Discrete distribtuions ---------------------
249 
258  class Discrete
259  {
260  public:
266  typedef unsigned long int result_type;
267 
271  Discrete(void);
272 
276  virtual ~Discrete(void);
277 
288  void seed(unsigned long s) const YAT_DEPRECATE;
289 
298  unsigned long seed_from_devurandom(void) YAT_DEPRECATE;
299 
303  virtual result_type operator()(void) const = 0;
304 
305  protected:
308  };
309 
317  class Binomial : public Discrete
318  {
319  public:
327  Binomial(double p, unsigned int n);
328 
332  unsigned long operator()(void) const;
333  private:
334  double p_;
335  unsigned int n_;
336  };
337 
341  class DiscreteGeneral : public Discrete
342  {
343  public:
349  explicit DiscreteGeneral(const statistics::Histogram& hist);
350 
357 
361  ~DiscreteGeneral(void);
362 
371  unsigned long operator()(void) const;
372 
378  DiscreteGeneral& operator=(const DiscreteGeneral&);
379 
380  private:
381  void free(void);
382  void preproc(void);
383 
384  gsl_ran_discrete_t* gen_;
385  std::vector<double> p_;
386  };
387 
401  : public Discrete,
402  public std::unary_function<unsigned long, unsigned long>
403  {
404  public:
418  explicit DiscreteUniform(unsigned long n=0);
419 
429  unsigned long operator()(void) const;
430 
441  unsigned long operator()(unsigned long n) const;
442 
443  private:
444  unsigned long range_;
445  };
446 
447 
458  class Geometric : public Discrete
459  {
460  public:
466  Geometric(double p);
467 
468  /*
469  \return a number from Geomtric distribution
470  */
471  unsigned long operator()(void) const;
472 
478  unsigned long operator()(double p) const;
479 
480  private:
481  double p_;
482  };
483 
484 
492  class HyperGeometric : public Discrete
493  {
494  public:
498  HyperGeometric(void);
499 
506  HyperGeometric(unsigned int n1, unsigned int n2, unsigned int t);
507 
512  unsigned long int operator()(void) const;
513 
518  unsigned long int operator()(unsigned int n1, unsigned int n2,
519  unsigned int t) const;
520  private:
521  unsigned int n1_;
522  unsigned int n2_;
523  unsigned int t_;
524  };
525 
537  {
538  public:
543 
550  NegativeHyperGeometric(unsigned int n1, unsigned int n2, unsigned int t);
551 
556  unsigned long int operator()(void) const;
557 
562  unsigned long int operator()(unsigned int n1, unsigned int n2,
563  unsigned int t) const;
564  private:
565  unsigned int n1_;
566  unsigned int n2_;
567  unsigned int t_;
568  };
569 
570 
586  class Poisson : public Discrete
587  {
588  public:
594  explicit Poisson(const double m=1);
595 
599  unsigned long operator()(void) const;
600 
607  unsigned long operator()(const double m) const;
608 
609  private:
610  double m_;
611  };
612 
613  // --------------------- Continuous distribtuions ---------------------
614 
621  {
622  public:
628  typedef double result_type;
629 
633  Continuous(void);
634 
638  virtual ~Continuous(void);
639 
650  void seed(unsigned long s) const YAT_DEPRECATE;
651 
660  unsigned long seed_from_devurandom(void) YAT_DEPRECATE;
661 
665  virtual result_type operator()(void) const = 0;
666 
667  protected:
670  };
671 
672  // ContinuousUniform is declared before ContinuousGeneral to avoid
673  // forward declaration
685  {
686  public:
687  double operator()(void) const;
688  };
689 
694  {
695  public:
701  explicit ContinuousGeneral(const statistics::Histogram& hist);
702 
711  double operator()(void) const;
712 
713  private:
714  const DiscreteGeneral discrete_;
715  const statistics::Histogram hist_;
717  };
718 
727  class Exponential : public Continuous
728  {
729  public:
735  explicit Exponential(const double m=1);
736 
740  double operator()(void) const;
741 
748  double operator()(const double m) const;
749 
750  private:
751  double m_;
752  };
753 
767  class Gaussian : public Continuous
768  {
769  public:
776  explicit Gaussian(const double s=1, const double m=0);
777 
781  double operator()(void) const;
782 
789  double operator()(const double s) const;
790 
797  double operator()(const double s, const double m) const;
798 
799  private:
800  double m_;
801  double s_;
802  };
803 
814  template<typename RandomAccessIterator>
815  void random_shuffle(RandomAccessIterator first, RandomAccessIterator last)
816  {
817  DiscreteUniform rnd;
818  std::random_shuffle(first, last, rnd);
819  }
820 
821 }}} // of namespace random, yat, and theplu
822 
823 #endif
General.
Definition: random.h:341
const gsl_rng * rng(void) const
virtual ~Discrete(void)
The destructor.
Discrete(void)
Constructor.
Discrete random number distributions.
Definition: random.h:258
Generates numbers from a histogram in a continuous manner.
Definition: random.h:693
Random Number Generator.
Definition: random.h:86
void seed(unsigned long s) const
Set the seed s for the rng.
Definition: random.h:492
unsigned long int result_type
Definition: random.h:266
Discrete uniform distribution.
Definition: random.h:400
~RNG_state(void)
Destructor.
Class holding state of a random generator.
Definition: random.h:209
Generator of random numbers from an exponential distribution.
Definition: random.h:727
unsigned long seed_from_devurandom(void)
Seed the rng using the /dev/urandom device.
Poisson Distribution.
Definition: random.h:586
Uniform distribution.
Definition: random.h:684
Geomtric Distribution.
Definition: random.h:458
void random_shuffle(RandomAccessIterator first, RandomAccessIterator last)
Convenience function to shuffle a range with singleton RNG.
Definition: random.h:815
RNG_state & operator=(const RNG_state &)
std::string name(void) const
Returns the name of the random number generator.
RNG_state(const RNG *)
Constructor.
Histograms provide a convenient way of presenting the distribution of a set of data.
Definition: Histogram.h:53
virtual result_type operator()(void) const =0
unsigned long max(void) const
Returns the largest number that the random number generator can return.
int set_state(const RNG_state &)
Set the state to state.
Continuous random number distributions.
Definition: random.h:620
void seed(unsigned long s) const
Set the seed to s.
unsigned long min(void) const
Returns the smallest number that the random number generator can return.
Binomial distribution.
Definition: random.h:317
const gsl_rng * rng(void) const
static RNG * instance(void)
Get an instance of the Random Number Generator.
RNG * rng_
GSL random gererator.
Definition: random.h:307
double result_type
Definition: random.h:628
Gaussian distribution.
Definition: random.h:767
unsigned long seed_from_devurandom(void)
Set the seed using the /dev/urandom device.

Generated on Tue Sep 26 2017 02:33:29 for yat by  doxygen 1.8.5