yat  0.15.2pre
random.h
1 #ifndef _theplu_yat_random_
2 #define _theplu_yat_random_
3 
4 // $Id: random.h 3591 2017-01-20 02:19:49Z 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 
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 
410  : public Discrete,
411  public std::unary_function<unsigned long, unsigned long>
412  {
413  public:
427  explicit DiscreteUniform(unsigned long n=0);
428 
438  unsigned long operator()(void) const;
439 
450  unsigned long operator()(unsigned long n) const;
451 
452  private:
453  unsigned long range_;
454  };
455 
456 
467  class Geometric : public Discrete
468  {
469  public:
475  Geometric(double p);
476 
477  /*
478  \return a number from Geomtric distribution
479  */
480  unsigned long operator()(void) const;
481 
487  unsigned long operator()(double p) const;
488 
489  private:
490  double p_;
491  };
492 
493 
501  class HyperGeometric : public Discrete
502  {
503  public:
507  HyperGeometric(void);
508 
515  HyperGeometric(unsigned int n1, unsigned int n2, unsigned int t);
516 
521  unsigned long int operator()(void) const;
522 
527  unsigned long int operator()(unsigned int n1, unsigned int n2,
528  unsigned int t) const;
529  private:
530  unsigned int n1_;
531  unsigned int n2_;
532  unsigned int t_;
533  };
534 
546  {
547  public:
552 
559  NegativeHyperGeometric(unsigned int n1, unsigned int n2, unsigned int t);
560 
565  unsigned long int operator()(void) const;
566 
571  unsigned long int operator()(unsigned int n1, unsigned int n2,
572  unsigned int t) const;
573  private:
574  unsigned int n1_;
575  unsigned int n2_;
576  unsigned int t_;
577  };
578 
579 
595  class Poisson : public Discrete
596  {
597  public:
603  explicit Poisson(const double m=1);
604 
608  unsigned long operator()(void) const;
609 
616  unsigned long operator()(const double m) const;
617 
618  private:
619  double m_;
620  };
621 
622  // --------------------- Continuous distribtuions ---------------------
623 
630  {
631  public:
637  typedef double result_type;
638 
642  Continuous(void);
643 
647  virtual ~Continuous(void);
648 
659  void seed(unsigned long s) const YAT_DEPRECATE;
660 
669  unsigned long seed_from_devurandom(void) YAT_DEPRECATE;
670 
674  virtual result_type operator()(void) const = 0;
675 
676  protected:
678  RNG* rng_;
679  };
680 
681  // ContinuousUniform is declared before ContinuousGeneral to avoid
682  // forward declaration
694  {
695  public:
696  double operator()(void) const;
697  };
698 
703  {
704  public:
710  explicit ContinuousGeneral(const statistics::Histogram& hist);
711 
720  double operator()(void) const;
721 
722  private:
723  const DiscreteGeneral discrete_;
724  const statistics::Histogram hist_;
726  };
727 
736  class Exponential : public Continuous
737  {
738  public:
744  explicit Exponential(const double m=1);
745 
749  double operator()(void) const;
750 
757  double operator()(const double m) const;
758 
759  private:
760  double m_;
761  };
762 
776  class Gaussian : public Continuous
777  {
778  public:
785  explicit Gaussian(const double s=1, const double m=0);
786 
790  double operator()(void) const;
791 
798  double operator()(const double s) const;
799 
806  double operator()(const double s, const double m) const;
807 
808  private:
809  double m_;
810  double s_;
811  };
812 
823  template<typename RandomAccessIterator>
824  void random_shuffle(RandomAccessIterator first, RandomAccessIterator last)
825  {
826  DiscreteUniform rnd;
827  std::random_shuffle(first, last, rnd);
828  }
829 
830 }}} // of namespace random, yat, and theplu
831 
832 #endif
General.
Definition: random.h:350
const gsl_rng * rng(void) const
Discrete random number distributions.
Definition: random.h:267
Generates numbers from a histogram in a continuous manner.
Definition: random.h:702
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:501
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:736
unsigned long seed_from_devurandom(void)
Seed the rng using the /dev/urandom device.
Poisson Distribution.
Definition: random.h:595
Uniform distribution.
Definition: random.h:693
Geomtric Distribution.
Definition: random.h:467
void random_shuffle(RandomAccessIterator first, RandomAccessIterator last)
Convenience function to shuffle a range with singleton RNG.
Definition: random.h:824
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:629
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:637
Gaussian distribution.
Definition: random.h:776

Generated on Fri Jul 13 2018 02:33:27 for yat by  doxygen 1.8.11