yat  0.19.3pre
random.h
1 #ifndef _theplu_yat_random_
2 #define _theplu_yat_random_
3 
4 // $Id: random.h 4010 2020-10-19 02:33:47Z 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:
349  DiscreteGeneral(void);
350 
356  explicit DiscreteGeneral(const statistics::Histogram& hist);
357 
364 
371 
375  ~DiscreteGeneral(void);
376 
385  unsigned long operator()(void) const;
386 
393 
400 
401  private:
402  void free(void);
403  void preproc(void);
404 
405  gsl_ran_discrete_t* gen_;
406  std::vector<double> p_;
407  friend void swap(DiscreteGeneral& lhs, DiscreteGeneral& rhs);
408  };
409 
415  void swap(DiscreteGeneral& lhs, DiscreteGeneral& rhs);
416 
429  class DiscreteUniform : public Discrete
430  {
431  public:
433  typedef unsigned long argument_type;
434 
448  explicit DiscreteUniform(unsigned long n=0);
449 
459  unsigned long operator()(void) const;
460 
471  unsigned long operator()(unsigned long n) const;
472 
481  unsigned long int min(void) const;
482 
491  unsigned long int max(void) const;
492 
493  private:
494  unsigned long range_;
495  };
496 
497 
508  class Geometric : public Discrete
509  {
510  public:
516  Geometric(double p);
517 
518  /*
519  \return a number from Geomtric distribution
520  */
521  unsigned long operator()(void) const;
522 
528  unsigned long operator()(double p) const;
529 
530  private:
531  double p_;
532  };
533 
534 
542  class HyperGeometric : public Discrete
543  {
544  public:
548  HyperGeometric(void);
549 
556  HyperGeometric(unsigned int n1, unsigned int n2, unsigned int t);
557 
562  unsigned long int operator()(void) const;
563 
568  unsigned long int operator()(unsigned int n1, unsigned int n2,
569  unsigned int t) const;
570  private:
571  unsigned int n1_;
572  unsigned int n2_;
573  unsigned int t_;
574  };
575 
587  {
588  public:
593 
600  NegativeHyperGeometric(unsigned int n1, unsigned int n2, unsigned int t);
601 
606  unsigned long int operator()(void) const;
607 
612  unsigned long int operator()(unsigned int n1, unsigned int n2,
613  unsigned int t) const;
614  private:
615  unsigned int n1_;
616  unsigned int n2_;
617  unsigned int t_;
618  };
619 
620 
636  class Poisson : public Discrete
637  {
638  public:
644  explicit Poisson(const double m=1);
645 
649  unsigned long operator()(void) const;
650 
657  unsigned long operator()(const double m) const;
658 
659  private:
660  double m_;
661  };
662 
663  // --------------------- Continuous distribtuions ---------------------
664 
671  {
672  public:
678  typedef double result_type;
679 
683  Continuous(void);
684 
688  virtual ~Continuous(void);
689 
700  void seed(unsigned long s) const YAT_DEPRECATE;
701 
710  unsigned long seed_from_devurandom(void) YAT_DEPRECATE;
711 
715  virtual result_type operator()(void) const = 0;
716 
717  protected:
720  };
721 
722  // ContinuousUniform is declared before ContinuousGeneral to avoid
723  // forward declaration
735  {
736  public:
737  double operator()(void) const;
738  };
739 
744  {
745  public:
751  explicit ContinuousGeneral(const statistics::Histogram& hist);
752 
761  double operator()(void) const;
762 
763  private:
764  const DiscreteGeneral discrete_;
765  const statistics::Histogram hist_;
767  };
768 
777  class Exponential : public Continuous
778  {
779  public:
785  explicit Exponential(const double m=1);
786 
790  double operator()(void) const;
791 
798  double operator()(const double m) const;
799 
800  private:
801  double m_;
802  };
803 
817  class Gaussian : public Continuous
818  {
819  public:
826  explicit Gaussian(const double s=1, const double m=0);
827 
831  double operator()(void) const;
832 
839  double operator()(const double s) const;
840 
847  double operator()(const double s, const double m) const;
848 
849  private:
850  double m_;
851  double s_;
852  };
853 
864  template<typename RandomAccessIterator>
865  void random_shuffle(RandomAccessIterator first, RandomAccessIterator last)
866  {
867  DiscreteUniform rnd;
868  std::random_shuffle(first, last, rnd);
869  }
870 
871 }}} // of namespace random, yat, and theplu
872 
873 #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:433
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:743
RNG * rng_
pointer to GSL random generator
Definition: random.h:719
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:542
unsigned long int result_type
Definition: random.h:265
Discrete uniform distribution.
Definition: random.h:429
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:777
unsigned long seed_from_devurandom(void)
Seed the rng using the /dev/urandom device.
Poisson Distribution.
Definition: random.h:636
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:734
Geomtric Distribution.
Definition: random.h:508
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:865
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:670
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:678
virtual ~Continuous(void)
The destructor.
Gaussian distribution.
Definition: random.h:817
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 Fri Aug 26 2022 08:41:29 for yat by  doxygen 1.8.14