yat  0.21pre
random.h
1 #ifndef _theplu_yat_random_
2 #define _theplu_yat_random_
3 
4 // $Id: random.h 4252 2022-11-18 02:54:04Z 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  Copyright (C) 2022 Jari Häkkinen, Peter Johansson
10 
11  This file is part of the yat library, http://dev.thep.lu.se/yat
12 
13  The yat library is free software; you can redistribute it and/or
14  modify it under the terms of the GNU General Public License as
15  published by the Free Software Foundation; either version 3 of the
16  License, or (at your option) any later version.
17 
18  The yat library is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  General Public License for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with yat. If not, see <http://www.gnu.org/licenses/>.
25 */
26 
28 
29 #include "yat/statistics/Histogram.h"
30 #include "yat/utility/deprecate.h"
31 
32 #include <boost/concept_check.hpp>
33 
34 #include <gsl/gsl_rng.h>
35 #include <gsl/gsl_randist.h>
36 
37 #include <algorithm>
38 #include <atomic>
39 #include <memory>
40 #include <mutex>
41 #include <random>
42 #include <string>
43 #include <vector>
44 
45 namespace theplu {
46 namespace yat {
47 namespace random {
48 
49  //forward declaration
50  class RNG_state;
51 
87  class RNG
88  {
89  public:
90 
108  static RNG* instance(void);
109 
114  unsigned long max(void) const;
115 
120  unsigned long min(void) const;
121 
125  std::string name(void) const;
126 
134  const gsl_rng* rng(void) const;
135 
149  void seed(unsigned long s) const;
150 
159  unsigned long seed_from_devurandom(void);
160 
172  // return int for backward compatibility with yat 0.8
173  int set_state(const RNG_state&);
174 
175  private:
176  RNG(void);
177 
185  RNG(const RNG&);
186 
191  RNG& operator=(const RNG&);
192 
193  virtual ~RNG(void);
194  void rng_alloc(void) const;
195 
196  static std::atomic<RNG*> instance_;
197  // holds one gsl_rng per thread. Access through rng(void) so a
198  // gsl_rng is allocated if necessary.
199  static thread_local std::unique_ptr<gsl_rng, void(*)(gsl_rng*)> rng_;
200  mutable unsigned long seed_;
201  // guard needs to be mutable because major mission for it is to protect seed_ against multi-access, and seed_ is mutable...
202  mutable std::mutex mutex_;
203  static std::mutex init_mutex_;
204  };
205 
206 
210  class RNG_state
211  {
212  public:
216  explicit RNG_state(const RNG*);
217 
223  RNG_state(const RNG_state&);
224 
228  ~RNG_state(void);
229 
233  const gsl_rng* rng(void) const;
234 
240  RNG_state& operator=(const RNG_state&);
241 
242  private:
243  gsl_rng* rng_;
244 
245  void clone(const gsl_rng&);
246  };
247 
248 
249  // --------------------- Discrete distribtuions ---------------------
250 
259  class Discrete
260  {
261  public:
267  typedef unsigned long int result_type;
268 
272  Discrete(void);
273 
277  virtual ~Discrete(void);
278 
289  void seed(unsigned long s) const YAT_DEPRECATE;
290 
299  unsigned long seed_from_devurandom(void) YAT_DEPRECATE;
300 
304  virtual result_type operator()(void) const = 0;
305 
306  protected:
309  };
310 
318  class Binomial : public Discrete
319  {
320  public:
328  Binomial(double p, unsigned int n);
329 
333  unsigned long operator()(void) const;
334  private:
335  double p_;
336  unsigned int n_;
337  };
338 
342  class DiscreteGeneral : public Discrete
343  {
344  public:
351  DiscreteGeneral(void);
352 
358  explicit DiscreteGeneral(const statistics::Histogram& hist);
359 
366 
373 
377  ~DiscreteGeneral(void);
378 
387  unsigned long operator()(void) const;
388 
395 
402 
403  private:
404  void free(void);
405  void preproc(void);
406 
407  gsl_ran_discrete_t* gen_;
408  std::vector<double> p_;
409  friend void swap(DiscreteGeneral& lhs, DiscreteGeneral& rhs);
410  };
411 
417  void swap(DiscreteGeneral& lhs, DiscreteGeneral& rhs);
418 
431  class DiscreteUniform : public Discrete
432  {
433  public:
435  typedef unsigned long argument_type;
436 
450  explicit DiscreteUniform(unsigned long n=0);
451 
461  unsigned long operator()(void) const;
462 
473  unsigned long operator()(unsigned long n) const;
474 
483  unsigned long int min(void) const;
484 
493  unsigned long int max(void) const;
494 
495  private:
496  unsigned long range_;
497  };
498 
499 
510  class Geometric : public Discrete
511  {
512  public:
518  Geometric(double p);
519 
520  /*
521  \return a number from Geomtric distribution
522  */
523  unsigned long operator()(void) const;
524 
530  unsigned long operator()(double p) const;
531 
532  private:
533  double p_;
534  };
535 
536 
544  class HyperGeometric : public Discrete
545  {
546  public:
550  HyperGeometric(void);
551 
558  HyperGeometric(unsigned int n1, unsigned int n2, unsigned int t);
559 
564  unsigned long int operator()(void) const;
565 
570  unsigned long int operator()(unsigned int n1, unsigned int n2,
571  unsigned int t) const;
572  private:
573  unsigned int n1_;
574  unsigned int n2_;
575  unsigned int t_;
576  };
577 
589  {
590  public:
595 
602  NegativeHyperGeometric(unsigned int n1, unsigned int n2, unsigned int t);
603 
608  unsigned long int operator()(void) const;
609 
614  unsigned long int operator()(unsigned int n1, unsigned int n2,
615  unsigned int t) const;
616  private:
617  unsigned int n1_;
618  unsigned int n2_;
619  unsigned int t_;
620  };
621 
622 
638  class Poisson : public Discrete
639  {
640  public:
646  explicit Poisson(const double m=1);
647 
651  unsigned long operator()(void) const;
652 
659  unsigned long operator()(const double m) const;
660 
661  private:
662  double m_;
663  };
664 
678  class NegativeBinomial : public Discrete
679  {
680  public:
687  NegativeBinomial(double p, double r);
688 
692  unsigned long operator()(void) const;
693 
697  unsigned long operator()(double p, double r) const;
698 
699  private:
700  double p_;
701  double r_;
702  };
703 
704  // --------------------- Continuous distribtuions ---------------------
705 
712  {
713  public:
719  typedef double result_type;
720 
724  Continuous(void);
725 
729  virtual ~Continuous(void);
730 
741  void seed(unsigned long s) const YAT_DEPRECATE;
742 
751  unsigned long seed_from_devurandom(void) YAT_DEPRECATE;
752 
756  virtual result_type operator()(void) const = 0;
757 
758  protected:
761  };
762 
763  // ContinuousUniform is declared before ContinuousGeneral to avoid
764  // forward declaration
776  {
777  public:
778  double operator()(void) const;
779  };
780 
785  {
786  public:
792  explicit ContinuousGeneral(const statistics::Histogram& hist);
793 
802  double operator()(void) const;
803 
804  private:
805  const DiscreteGeneral discrete_;
806  const statistics::Histogram hist_;
808  };
809 
818  class Exponential : public Continuous
819  {
820  public:
826  explicit Exponential(const double m=1);
827 
831  double operator()(void) const;
832 
839  double operator()(const double m) const;
840 
841  private:
842  double m_;
843  };
844 
858  class Gaussian : public Continuous
859  {
860  public:
867  explicit Gaussian(const double s=1, const double m=0);
868 
872  double operator()(void) const;
873 
880  double operator()(const double s) const;
881 
888  double operator()(const double s, const double m) const;
889 
890  private:
891  double m_;
892  double s_;
893  };
894 
905  template<typename RandomAccessIterator>
906  void random_shuffle(RandomAccessIterator first, RandomAccessIterator last)
907  {
908  DiscreteUniform rnd;
909  std::shuffle(first, last, std::default_random_engine(rnd()));
910  }
911 
912 }}} // of namespace random, yat, and theplu
913 
914 #endif
General.
Definition: random.h:342
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:435
void seed(unsigned long s) const
Set the seed s for the rng.
Discrete(void)
Constructor.
Discrete random number distributions.
Definition: random.h:259
Generates numbers from a histogram in a continuous manner.
Definition: random.h:784
RNG * rng_
pointer to GSL random generator
Definition: random.h:760
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:87
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:544
unsigned long int result_type
Definition: random.h:267
Discrete uniform distribution.
Definition: random.h:431
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:210
Generator of random numbers from an exponential distribution.
Definition: random.h:818
unsigned long seed_from_devurandom(void)
Seed the rng using the /dev/urandom device.
Poisson Distribution.
Definition: random.h:638
NegativeBinomial(double p, double r)
Constructor.
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:775
Geomtric Distribution.
Definition: random.h:510
Gaussian(const double s=1, const double m=0)
Constructor.
Negative Binomial Distribution.
Definition: random.h:678
void random_shuffle(RandomAccessIterator first, RandomAccessIterator last)
Convenience function to shuffle a range with singleton RNG.
Definition: random.h:906
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.
unsigned long operator()(void) const
Continuous random number distributions.
Definition: random.h:711
unsigned long operator()(void) const
Geometric(double p)
Constructor.
Binomial distribution.
Definition: random.h:318
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:308
double result_type
Definition: random.h:719
virtual ~Continuous(void)
The destructor.
Gaussian distribution.
Definition: random.h:858
HyperGeometric(void)
Defaul constructor.
unsigned long int operator()(void) const
unsigned long seed_from_devurandom(void)
Set the seed using the /dev/urandom device.

Generated on Wed Jan 25 2023 03:34:29 for yat by  doxygen 1.8.14