yat  0.13.2pre
random.h
1 #ifndef _theplu_yat_random_
2 #define _theplu_yat_random_
3 
4 // $Id: random.h 3330 2014-10-14 08:03:25Z peter $
5 
6 /*
7  Copyright (C) 2005, 2006, 2007, 2008 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 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 
26 #include "yat/statistics/Histogram.h"
27 #include "yat/utility/deprecate.h"
28 
29 // always include this before <boost/excpetion_ptr.hpp> indirectly below
30 #include "yat/utility/boost_exception_ptr.h"
31 
32 #include <boost/concept_check.hpp>
33 #include <boost/iterator/iterator_concepts.hpp>
34 #include <boost/thread.hpp>
35 #include <boost/thread/tss.hpp>
36 
37 #include <gsl/gsl_rng.h>
38 #include <gsl/gsl_randist.h>
39 
40 #include <algorithm>
41 #include <string>
42 #include <vector>
43 
44 namespace theplu {
45 namespace yat {
46 namespace random {
47 
48  //forward declaration
49  class RNG_state;
50 
86  class RNG
87  {
88  public:
89 
107  static RNG* instance(void);
108 
113  unsigned long max(void) const;
114 
119  unsigned long min(void) const;
120 
124  std::string name(void) const;
125 
133  const gsl_rng* rng(void) const;
134 
148  void seed(unsigned long s) const;
149 
158  unsigned long seed_from_devurandom(void);
159 
171  // return int for backward compatibility with yat 0.8
172  int set_state(const RNG_state&);
173 
174  private:
175  RNG(void);
176 
184  RNG(const RNG&);
185 
190  RNG& operator=(const RNG&);
191 
192  virtual ~RNG(void);
193  void rng_alloc(void) const;
194 
195  static RNG* instance_;
196  // holds one gsl_rng per thread. Access through rng(void) so a
197  // gsl_rng is allocated if necessary.
198  mutable boost::thread_specific_ptr<gsl_rng> rng_;
199  mutable unsigned long seed_;
200  // guard needs to be mutable because major mission for it is to protect seed_ against multi-access, and seed_ is mutable...
201  mutable boost::mutex 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 
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 
377  DiscreteGeneral& operator=(const DiscreteGeneral&);
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 
400  : public Discrete,
401  public std::unary_function<unsigned long, unsigned long>
402  {
403  public:
417  explicit DiscreteUniform(unsigned long n=0);
418 
428  unsigned long operator()(void) const;
429 
440  unsigned long operator()(unsigned long n) const;
441 
442  private:
443  unsigned long range_;
444  };
445 
461  class Poisson : public Discrete
462  {
463  public:
469  explicit Poisson(const double m=1);
470 
474  unsigned long operator()(void) const;
475 
482  unsigned long operator()(const double m) const;
483 
484  private:
485  double m_;
486  };
487 
488  // --------------------- Continuous distribtuions ---------------------
489 
496  {
497  public:
503  typedef double result_type;
504 
508  Continuous(void);
509 
513  virtual ~Continuous(void);
514 
525  void seed(unsigned long s) const YAT_DEPRECATE;
526 
535  unsigned long seed_from_devurandom(void) YAT_DEPRECATE;
536 
540  virtual result_type operator()(void) const = 0;
541 
542  protected:
545  };
546 
547  // ContinuousUniform is declared before ContinuousGeneral to avoid
548  // forward declaration
560  {
561  public:
562  double operator()(void) const;
563  };
564 
569  {
570  public:
576  explicit ContinuousGeneral(const statistics::Histogram& hist);
577 
586  double operator()(void) const;
587 
588  private:
589  const DiscreteGeneral discrete_;
590  const statistics::Histogram hist_;
592  };
593 
602  class Exponential : public Continuous
603  {
604  public:
610  explicit Exponential(const double m=1);
611 
615  double operator()(void) const;
616 
623  double operator()(const double m) const;
624 
625  private:
626  double m_;
627  };
628 
642  class Gaussian : public Continuous
643  {
644  public:
651  explicit Gaussian(const double s=1, const double m=0);
652 
656  double operator()(void) const;
657 
664  double operator()(const double s) const;
665 
672  double operator()(const double s, const double m) const;
673 
674  private:
675  double m_;
676  double s_;
677  };
678 
690  template<typename RandomAccessIterator>
691  void random_shuffle(RandomAccessIterator first, RandomAccessIterator last)
692  {
693  typedef RandomAccessIterator rai;
694  BOOST_CONCEPT_ASSERT((boost_concepts::SwappableIterator<rai>));
695  BOOST_CONCEPT_ASSERT((boost_concepts::RandomAccessTraversal<rai>));
696  DiscreteUniform rnd;
697  std::random_shuffle(first, last, rnd);
698  }
699 
700 }}} // of namespace random, yat, and theplu
701 
702 #endif
General.
Definition: random.h:340
const gsl_rng * rng(void) const
virtual ~Discrete(void)
The destructor.
Discrete(void)
Constructor.
Discrete random number distributions.
Definition: random.h:257
Generates numbers from a histogram in a continuous manner.
Definition: random.h:568
Random Number Generator.
Definition: random.h:86
void seed(unsigned long s) const
Set the seed s for the rng.
unsigned long int result_type
Definition: random.h:265
Discrete uniform distribution.
Definition: random.h:399
~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:602
unsigned long seed_from_devurandom(void)
Seed the rng using the /dev/urandom device.
Poisson Distribution.
Definition: random.h:461
Uniform distribution.
Definition: random.h:559
void random_shuffle(RandomAccessIterator first, RandomAccessIterator last)
Convenience function to shuffle a range with singleton RNG.
Definition: random.h:691
RNG_state & operator=(const RNG_state &)
std::string name(void) const
Returns the name of the random number generator.
RNG_state(const RNG *)
Constructor.
Histograms provide a convenient way of presenting the distribution of a set of data.
Definition: Histogram.h:52
virtual result_type operator()(void) const =0
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:495
void seed(unsigned long s) const
Set the seed to s.
unsigned long min(void) const
Returns the smallest number that the random number generator can return.
Binomial distribution.
Definition: random.h:316
const gsl_rng * rng(void) const
static RNG * instance(void)
Get an instance of the Random Number Generator.
RNG * rng_
GSL random gererator.
Definition: random.h:306
double result_type
Definition: random.h:503
Gaussian distribution.
Definition: random.h:642
unsigned long seed_from_devurandom(void)
Set the seed using the /dev/urandom device.

Generated on Wed Jan 4 2017 02:23:07 for yat by  doxygen 1.8.5