yat  0.17.3pre
random.h
1 #ifndef _theplu_yat_random_
2 #define _theplu_yat_random_
3 
4 // $Id: random.h 3892 2020-03-27 09:12: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 // always include this before <boost/excpetion_ptr.hpp> indirectly below
32 #include "yat/utility/boost_exception_ptr.h"
33 
34 #include <boost/concept_check.hpp>
35 #include <boost/iterator/iterator_concepts.hpp>
36 #include <boost/thread.hpp>
37 #include <boost/thread/tss.hpp>
38 
39 #include <gsl/gsl_rng.h>
40 #include <gsl/gsl_randist.h>
41 
42 #include <algorithm>
43 #ifdef YAT_HAVE_ATOMIC
44 #include <atomic>
45 #endif
46 #include <string>
47 #include <vector>
48 
49 namespace theplu {
50 namespace yat {
51 namespace random {
52 
53  //forward declaration
54  class RNG_state;
55 
91  class RNG
92  {
93  public:
94 
112  static RNG* instance(void);
113 
118  unsigned long max(void) const;
119 
124  unsigned long min(void) const;
125 
129  std::string name(void) const;
130 
138  const gsl_rng* rng(void) const;
139 
153  void seed(unsigned long s) const;
154 
163  unsigned long seed_from_devurandom(void);
164 
176  // return int for backward compatibility with yat 0.8
177  int set_state(const RNG_state&);
178 
179  private:
180  RNG(void);
181 
189  RNG(const RNG&);
190 
195  RNG& operator=(const RNG&);
196 
197  virtual ~RNG(void);
198  void rng_alloc(void) const;
199 
200 #ifdef YAT_HAVE_ATOMIC
201  static std::atomic<RNG*> instance_;
202 #else
203  static RNG* instance_;
204 #endif
205  // holds one gsl_rng per thread. Access through rng(void) so a
206  // gsl_rng is allocated if necessary.
207  mutable boost::thread_specific_ptr<gsl_rng> rng_;
208  mutable unsigned long seed_;
209  // guard needs to be mutable because major mission for it is to protect seed_ against multi-access, and seed_ is mutable...
210  mutable boost::mutex mutex_;
211  static boost::mutex init_mutex_;
212  };
213 
214 
218  class RNG_state
219  {
220  public:
224  explicit RNG_state(const RNG*);
225 
231  RNG_state(const RNG_state&);
232 
236  ~RNG_state(void);
237 
241  const gsl_rng* rng(void) const;
242 
248  RNG_state& operator=(const RNG_state&);
249 
250  private:
251  gsl_rng* rng_;
252 
253  void clone(const gsl_rng&);
254  };
255 
256 
257  // --------------------- Discrete distribtuions ---------------------
258 
267  class Discrete
268  {
269  public:
275  typedef unsigned long int result_type;
276 
280  Discrete(void);
281 
285  virtual ~Discrete(void);
286 
297  void seed(unsigned long s) const YAT_DEPRECATE;
298 
307  unsigned long seed_from_devurandom(void) YAT_DEPRECATE;
308 
312  virtual result_type operator()(void) const = 0;
313 
314  protected:
316  RNG* rng_;
317  };
318 
326  class Binomial : public Discrete
327  {
328  public:
336  Binomial(double p, unsigned int n);
337 
341  unsigned long operator()(void) const;
342  private:
343  double p_;
344  unsigned int n_;
345  };
346 
350  class DiscreteGeneral : public Discrete
351  {
352  public:
358  explicit DiscreteGeneral(const statistics::Histogram& hist);
359 
366 
370  ~DiscreteGeneral(void);
371 
380  unsigned long operator()(void) const;
381 
387  DiscreteGeneral& operator=(const DiscreteGeneral&);
388 
389  private:
390  void free(void);
391  void preproc(void);
392 
393  gsl_ran_discrete_t* gen_;
394  std::vector<double> p_;
395  };
396 
409  class DiscreteUniform : public Discrete
410  {
411  public:
413  typedef unsigned long argument_type;
414 
428  explicit DiscreteUniform(unsigned long n=0);
429 
439  unsigned long operator()(void) const;
440 
451  unsigned long operator()(unsigned long n) const;
452 
453  private:
454  unsigned long range_;
455  };
456 
457 
468  class Geometric : public Discrete
469  {
470  public:
476  Geometric(double p);
477 
478  /*
479  \return a number from Geomtric distribution
480  */
481  unsigned long operator()(void) const;
482 
488  unsigned long operator()(double p) const;
489 
490  private:
491  double p_;
492  };
493 
494 
502  class HyperGeometric : public Discrete
503  {
504  public:
508  HyperGeometric(void);
509 
516  HyperGeometric(unsigned int n1, unsigned int n2, unsigned int t);
517 
522  unsigned long int operator()(void) const;
523 
528  unsigned long int operator()(unsigned int n1, unsigned int n2,
529  unsigned int t) const;
530  private:
531  unsigned int n1_;
532  unsigned int n2_;
533  unsigned int t_;
534  };
535 
547  {
548  public:
553 
560  NegativeHyperGeometric(unsigned int n1, unsigned int n2, unsigned int t);
561 
566  unsigned long int operator()(void) const;
567 
572  unsigned long int operator()(unsigned int n1, unsigned int n2,
573  unsigned int t) const;
574  private:
575  unsigned int n1_;
576  unsigned int n2_;
577  unsigned int t_;
578  };
579 
580 
596  class Poisson : public Discrete
597  {
598  public:
604  explicit Poisson(const double m=1);
605 
609  unsigned long operator()(void) const;
610 
617  unsigned long operator()(const double m) const;
618 
619  private:
620  double m_;
621  };
622 
623  // --------------------- Continuous distribtuions ---------------------
624 
631  {
632  public:
638  typedef double result_type;
639 
643  Continuous(void);
644 
648  virtual ~Continuous(void);
649 
660  void seed(unsigned long s) const YAT_DEPRECATE;
661 
670  unsigned long seed_from_devurandom(void) YAT_DEPRECATE;
671 
675  virtual result_type operator()(void) const = 0;
676 
677  protected:
679  RNG* rng_;
680  };
681 
682  // ContinuousUniform is declared before ContinuousGeneral to avoid
683  // forward declaration
695  {
696  public:
697  double operator()(void) const;
698  };
699 
704  {
705  public:
711  explicit ContinuousGeneral(const statistics::Histogram& hist);
712 
721  double operator()(void) const;
722 
723  private:
724  const DiscreteGeneral discrete_;
725  const statistics::Histogram hist_;
727  };
728 
737  class Exponential : public Continuous
738  {
739  public:
745  explicit Exponential(const double m=1);
746 
750  double operator()(void) const;
751 
758  double operator()(const double m) const;
759 
760  private:
761  double m_;
762  };
763 
777  class Gaussian : public Continuous
778  {
779  public:
786  explicit Gaussian(const double s=1, const double m=0);
787 
791  double operator()(void) const;
792 
799  double operator()(const double s) const;
800 
807  double operator()(const double s, const double m) const;
808 
809  private:
810  double m_;
811  double s_;
812  };
813 
824  template<typename RandomAccessIterator>
825  void random_shuffle(RandomAccessIterator first, RandomAccessIterator last)
826  {
827  DiscreteUniform rnd;
828  std::random_shuffle(first, last, rnd);
829  }
830 
831 }}} // of namespace random, yat, and theplu
832 
833 #endif
General.
Definition: random.h:350
const gsl_rng * rng(void) const
unsigned long argument_type
argument type is unsigned long int
Definition: random.h:413
Discrete random number distributions.
Definition: random.h:267
Generates numbers from a histogram in a continuous manner.
Definition: random.h:703
The Department of Theoretical Physics namespace as we define it.
Random Number Generator.
Definition: random.h:91
void seed(unsigned long s) const
Set the seed s for the rng.
Definition: random.h:502
unsigned long int result_type
Definition: random.h:275
Discrete uniform distribution.
Definition: random.h:409
Class holding state of a random generator.
Definition: random.h:218
Generator of random numbers from an exponential distribution.
Definition: random.h:737
unsigned long seed_from_devurandom(void)
Seed the rng using the /dev/urandom device.
Poisson Distribution.
Definition: random.h:596
Uniform distribution.
Definition: random.h:694
Geomtric Distribution.
Definition: random.h:468
void random_shuffle(RandomAccessIterator first, RandomAccessIterator last)
Convenience function to shuffle a range with singleton RNG.
Definition: random.h:825
std::string name(void) const
Returns the name of the random number generator.
Histograms provide a convenient way of presenting the distribution of a set of data.
Definition: Histogram.h:53
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:630
unsigned long min(void) const
Returns the smallest number that the random number generator can return.
Binomial distribution.
Definition: random.h:326
static RNG * instance(void)
Get an instance of the Random Number Generator.
double result_type
Definition: random.h:638
Gaussian distribution.
Definition: random.h:777

Generated on Thu Aug 27 2020 03:33:18 for yat by  doxygen 1.8.11