yat  0.18.2pre
random.h
1 #ifndef _theplu_yat_random_
2 #define _theplu_yat_random_
3 
4 // $Id: random.h 3959 2020-07-30 12:02:27Z 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, 2020 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 
27 
28 #include "yat/statistics/Histogram.h"
29 #include "yat/utility/deprecate.h"
30 
31 #include <boost/concept_check.hpp>
32 
33 #include <gsl/gsl_rng.h>
34 #include <gsl/gsl_randist.h>
35 
36 #include <algorithm>
37 #include <atomic>
38 #include <memory>
39 #include <mutex>
40 #include <string>
41 #include <vector>
42 
43 namespace theplu {
44 namespace yat {
45 namespace random {
46 
47  //forward declaration
48  class RNG_state;
49 
85  class RNG
86  {
87  public:
88 
106  static RNG* instance(void);
107 
112  unsigned long max(void) const;
113 
118  unsigned long min(void) const;
119 
123  std::string name(void) const;
124 
132  const gsl_rng* rng(void) const;
133 
147  void seed(unsigned long s) const;
148 
157  unsigned long seed_from_devurandom(void);
158 
170  // return int for backward compatibility with yat 0.8
171  int set_state(const RNG_state&);
172 
173  private:
174  RNG(void);
175 
183  RNG(const RNG&);
184 
189  RNG& operator=(const RNG&);
190 
191  virtual ~RNG(void);
192  void rng_alloc(void) const;
193 
194  static std::atomic<RNG*> instance_;
195  // holds one gsl_rng per thread. Access through rng(void) so a
196  // gsl_rng is allocated if necessary.
197  static thread_local std::unique_ptr<gsl_rng, void(*)(gsl_rng*)> rng_;
198  mutable unsigned long seed_;
199  // guard needs to be mutable because major mission for it is to protect seed_ against multi-access, and seed_ is mutable...
200  mutable std::mutex mutex_;
201  static std::mutex init_mutex_;
202  };
203 
204 
208  class RNG_state
209  {
210  public:
214  explicit RNG_state(const RNG*);
215 
221  RNG_state(const RNG_state&);
222 
226  ~RNG_state(void);
227 
231  const gsl_rng* rng(void) const;
232 
238  RNG_state& operator=(const RNG_state&);
239 
240  private:
241  gsl_rng* rng_;
242 
243  void clone(const gsl_rng&);
244  };
245 
246 
247  // --------------------- Discrete distribtuions ---------------------
248 
257  class Discrete
258  {
259  public:
265  typedef unsigned long int result_type;
266 
270  Discrete(void);
271 
275  virtual ~Discrete(void);
276 
287  void seed(unsigned long s) const YAT_DEPRECATE;
288 
297  unsigned long seed_from_devurandom(void) YAT_DEPRECATE;
298 
302  virtual result_type operator()(void) const = 0;
303 
304  protected:
307  };
308 
316  class Binomial : public Discrete
317  {
318  public:
326  Binomial(double p, unsigned int n);
327 
331  unsigned long operator()(void) const;
332  private:
333  double p_;
334  unsigned int n_;
335  };
336 
340  class DiscreteGeneral : public Discrete
341  {
342  public:
348  explicit DiscreteGeneral(const statistics::Histogram& hist);
349 
356 
360  ~DiscreteGeneral(void);
361 
370  unsigned long operator()(void) const;
371 
378 
379  private:
380  void free(void);
381  void preproc(void);
382 
383  gsl_ran_discrete_t* gen_;
384  std::vector<double> p_;
385  };
386 
399  class DiscreteUniform : public Discrete
400  {
401  public:
403  typedef unsigned long argument_type;
404 
418  explicit DiscreteUniform(unsigned long n=0);
419 
429  unsigned long operator()(void) const;
430 
441  unsigned long operator()(unsigned long n) const;
442 
451  unsigned long int min(void) const;
452 
461  unsigned long int max(void) const;
462 
463  private:
464  unsigned long range_;
465  };
466 
467 
478  class Geometric : public Discrete
479  {
480  public:
486  Geometric(double p);
487 
488  /*
489  \return a number from Geomtric distribution
490  */
491  unsigned long operator()(void) const;
492 
498  unsigned long operator()(double p) const;
499 
500  private:
501  double p_;
502  };
503 
504 
512  class HyperGeometric : public Discrete
513  {
514  public:
518  HyperGeometric(void);
519 
526  HyperGeometric(unsigned int n1, unsigned int n2, unsigned int t);
527 
532  unsigned long int operator()(void) const;
533 
538  unsigned long int operator()(unsigned int n1, unsigned int n2,
539  unsigned int t) const;
540  private:
541  unsigned int n1_;
542  unsigned int n2_;
543  unsigned int t_;
544  };
545 
557  {
558  public:
563 
570  NegativeHyperGeometric(unsigned int n1, unsigned int n2, unsigned int t);
571 
576  unsigned long int operator()(void) const;
577 
582  unsigned long int operator()(unsigned int n1, unsigned int n2,
583  unsigned int t) const;
584  private:
585  unsigned int n1_;
586  unsigned int n2_;
587  unsigned int t_;
588  };
589 
590 
606  class Poisson : public Discrete
607  {
608  public:
614  explicit Poisson(const double m=1);
615 
619  unsigned long operator()(void) const;
620 
627  unsigned long operator()(const double m) const;
628 
629  private:
630  double m_;
631  };
632 
633  // --------------------- Continuous distribtuions ---------------------
634 
641  {
642  public:
648  typedef double result_type;
649 
653  Continuous(void);
654 
658  virtual ~Continuous(void);
659 
670  void seed(unsigned long s) const YAT_DEPRECATE;
671 
680  unsigned long seed_from_devurandom(void) YAT_DEPRECATE;
681 
685  virtual result_type operator()(void) const = 0;
686 
687  protected:
690  };
691 
692  // ContinuousUniform is declared before ContinuousGeneral to avoid
693  // forward declaration
705  {
706  public:
707  double operator()(void) const;
708  };
709 
714  {
715  public:
721  explicit ContinuousGeneral(const statistics::Histogram& hist);
722 
731  double operator()(void) const;
732 
733  private:
734  const DiscreteGeneral discrete_;
735  const statistics::Histogram hist_;
737  };
738 
747  class Exponential : public Continuous
748  {
749  public:
755  explicit Exponential(const double m=1);
756 
760  double operator()(void) const;
761 
768  double operator()(const double m) const;
769 
770  private:
771  double m_;
772  };
773 
787  class Gaussian : public Continuous
788  {
789  public:
796  explicit Gaussian(const double s=1, const double m=0);
797 
801  double operator()(void) const;
802 
809  double operator()(const double s) const;
810 
817  double operator()(const double s, const double m) const;
818 
819  private:
820  double m_;
821  double s_;
822  };
823 
834  template<typename RandomAccessIterator>
835  void random_shuffle(RandomAccessIterator first, RandomAccessIterator last)
836  {
837  DiscreteUniform rnd;
838  std::random_shuffle(first, last, rnd);
839  }
840 
841 }}} // of namespace random, yat, and theplu
842 
843 #endif
General.
Definition: random.h:340
virtual ~Discrete(void)
The destructor.
DiscreteGeneral & operator=(const DiscreteGeneral &)
Assignment operator.
Continuous(void)
Constructor.
unsigned long operator()(void) const
unsigned long argument_type
argument type is unsigned long int
Definition: random.h:403
void seed(unsigned long s) const
Set the seed s for the rng.
Discrete(void)
Constructor.
Discrete random number distributions.
Definition: random.h:257
Generates numbers from a histogram in a continuous manner.
Definition: random.h:713
RNG * rng_
pointer to GSL random generator
Definition: random.h:689
The Department of Theoretical Physics namespace as we define it.
NegativeHyperGeometric(void)
Default constructor.
unsigned long operator()(void) const
Random Number Generator.
Definition: random.h:85
virtual result_type operator()(void) const =0
unsigned long operator()(void) const
Get a random number.
unsigned long max(void) const
Returns the largest number that the random number generator can return.
ContinuousGeneral(const statistics::Histogram &hist)
Constructor.
unsigned long min(void) const
Returns the smallest number that the random number generator can return.
Definition: random.h:512
unsigned long int result_type
Definition: random.h:265
Discrete uniform distribution.
Definition: random.h:399
unsigned long operator()(void) const
double operator()(void) const
unsigned long int max(void) const
void seed(unsigned long s) const
Set the seed to s.
Poisson(const double m=1)
Constructor.
~RNG_state(void)
Destructor.
Class holding state of a random generator.
Definition: random.h:208
Generator of random numbers from an exponential distribution.
Definition: random.h:747
unsigned long seed_from_devurandom(void)
Seed the rng using the /dev/urandom device.
Poisson Distribution.
Definition: random.h:606
Exponential(const double m=1)
Constructor.
const gsl_rng * rng(void) const
unsigned long seed_from_devurandom(void)
Set the seed using the /dev/urandom device.
Uniform distribution.
Definition: random.h:704
Geomtric Distribution.
Definition: random.h:478
Gaussian(const double s=1, const double m=0)
Constructor.
void random_shuffle(RandomAccessIterator first, RandomAccessIterator last)
Convenience function to shuffle a range with singleton RNG.
Definition: random.h:835
RNG_state & operator=(const RNG_state &)
DiscreteUniform(unsigned long n=0)
Constructor.
RNG_state(const RNG *)
Constructor.
unsigned long int operator()(void) const
const gsl_rng * rng(void) const
Histograms provide a convenient way of presenting the distribution of a set of data.
Definition: Histogram.h:53
std::string name(void) const
Returns the name of the random number generator.
virtual result_type operator()(void) const =0
void seed(unsigned long s) const
Set the seed to s.
unsigned long int min(void) const
int set_state(const RNG_state &)
Set the state to state.
Continuous random number distributions.
Definition: random.h:640
unsigned long operator()(void) const
Geometric(double p)
Constructor.
Binomial distribution.
Definition: random.h:316
static RNG * instance(void)
Get an instance of the Random Number Generator.
Binomial(double p, unsigned int n)
Constructor.
RNG * rng_
GSL random gererator.
Definition: random.h:306
double result_type
Definition: random.h:648
virtual ~Continuous(void)
The destructor.
Gaussian distribution.
Definition: random.h:787
HyperGeometric(void)
Defaul constructor.
unsigned long int operator()(void) const
DiscreteGeneral(const statistics::Histogram &hist)
Constructor.
unsigned long seed_from_devurandom(void)
Set the seed using the /dev/urandom device.

Generated on Tue Sep 7 2021 17:32:32 for yat by  doxygen 1.8.14