yat  0.12.3pre
random.h
1 #ifndef _theplu_yat_random_
2 #define _theplu_yat_random_
3 
4 // $Id: random.h 3114 2013-11-10 23:51:47Z peter $
5 
6 /*
7  Copyright (C) 2005, 2006, 2007, 2008 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2009, 2010, 2011, 2012, 2013 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/thread.hpp>
34 #include <boost/thread/tss.hpp>
35 
36 #include <gsl/gsl_rng.h>
37 #include <gsl/gsl_randist.h>
38 
39 #include <algorithm>
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 RNG* instance_;
195  // holds one gsl_rng per thread. Access through rng(void) so a
196  // gsl_rng is allocated if necessary.
197  mutable boost::thread_specific_ptr<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 boost::mutex mutex_;
201  };
202 
203 
207  class RNG_state
208  {
209  public:
213  explicit RNG_state(const RNG*);
214 
220  RNG_state(const RNG_state&);
221 
225  ~RNG_state(void);
226 
230  const gsl_rng* rng(void) const;
231 
237  RNG_state& operator=(const RNG_state&);
238 
239  private:
240  gsl_rng* rng_;
241 
242  void clone(const gsl_rng&);
243  };
244 
245 
246  // --------------------- Discrete distribtuions ---------------------
247 
256  class Discrete
257  {
258  public:
264  typedef unsigned long int result_type;
265 
269  Discrete(void);
270 
274  virtual ~Discrete(void);
275 
286  void seed(unsigned long s) const YAT_DEPRECATE;
287 
296  unsigned long seed_from_devurandom(void) YAT_DEPRECATE;
297 
301  virtual result_type operator()(void) const = 0;
302 
303  protected:
306  };
307 
316  {
317  public:
325  Binomial(double p, unsigned int n);
326 
330  unsigned long operator()(void) const;
331  private:
332  double p_;
333  unsigned int n_;
334  };
335 
339  class DiscreteGeneral : public Discrete
340  {
341  public:
347  explicit DiscreteGeneral(const statistics::Histogram& hist);
348 
355 
359  ~DiscreteGeneral(void);
360 
369  unsigned long operator()(void) const;
370 
376  DiscreteGeneral& operator=(const DiscreteGeneral&);
377 
378  private:
379  void free(void);
380  void preproc(void);
381 
382  gsl_ran_discrete_t* gen_;
383  std::vector<double> p_;
384  };
385 
399  : public Discrete,
400  public std::unary_function<unsigned long, unsigned long>
401  {
402  public:
416  explicit DiscreteUniform(unsigned long n=0);
417 
427  unsigned long operator()(void) const;
428 
439  unsigned long operator()(unsigned long n) const;
440 
441  private:
442  unsigned long range_;
443  };
444 
460  class Poisson : public Discrete
461  {
462  public:
468  explicit Poisson(const double m=1);
469 
473  unsigned long operator()(void) const;
474 
481  unsigned long operator()(const double m) const;
482 
483  private:
484  double m_;
485  };
486 
487  // --------------------- Continuous distribtuions ---------------------
488 
495  {
496  public:
502  typedef double result_type;
503 
507  Continuous(void);
508 
512  virtual ~Continuous(void);
513 
524  void seed(unsigned long s) const YAT_DEPRECATE;
525 
534  unsigned long seed_from_devurandom(void) YAT_DEPRECATE;
535 
539  virtual result_type operator()(void) const = 0;
540 
541  protected:
544  };
545 
546  // ContinuousUniform is declared before ContinuousGeneral to avoid
547  // forward declaration
559  {
560  public:
561  double operator()(void) const;
562  };
563 
568  {
569  public:
575  explicit ContinuousGeneral(const statistics::Histogram& hist);
576 
585  double operator()(void) const;
586 
587  private:
588  const DiscreteGeneral discrete_;
589  const statistics::Histogram hist_;
591  };
592 
601  class Exponential : public Continuous
602  {
603  public:
609  explicit Exponential(const double m=1);
610 
614  double operator()(void) const;
615 
622  double operator()(const double m) const;
623 
624  private:
625  double m_;
626  };
627 
641  class Gaussian : public Continuous
642  {
643  public:
650  explicit Gaussian(const double s=1, const double m=0);
651 
655  double operator()(void) const;
656 
663  double operator()(const double s) const;
664 
671  double operator()(const double s, const double m) const;
672 
673  private:
674  double m_;
675  double s_;
676  };
677 
687  template<typename RandomAccessIterator>
688  void random_shuffle(RandomAccessIterator first, RandomAccessIterator last)
689  {
690  typedef RandomAccessIterator rai;
691  BOOST_CONCEPT_ASSERT((boost::Mutable_RandomAccessIterator<rai>));
692  DiscreteUniform rnd;
693  std::random_shuffle(first, last, rnd);
694  }
695 
696 }}} // of namespace random, yat, and theplu
697 
698 #endif
General.
Definition: random.h:339
const gsl_rng * rng(void) const
virtual ~Discrete(void)
The destructor.
Discrete(void)
Constructor.
Discrete random number distributions.
Definition: random.h:256
Generates numbers from a histogram in a continuous manner.
Definition: random.h:567
Random Number Generator.
Definition: random.h:85
void seed(unsigned long s) const
Set the seed s for the rng.
unsigned long int result_type
Definition: random.h:264
Discrete uniform distribution.
Definition: random.h:398
~RNG_state(void)
Destructor.
Class holding state of a random generator.
Definition: random.h:207
Generator of random numbers from an exponential distribution.
Definition: random.h:601
unsigned long seed_from_devurandom(void)
Seed the rng using the /dev/urandom device.
Poisson Distribution.
Definition: random.h:460
Uniform distribution.
Definition: random.h:558
void random_shuffle(RandomAccessIterator first, RandomAccessIterator last)
Convenience function to shuffle a range with singleton RNG.
Definition: random.h:688
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:494
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:315
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:305
double result_type
Definition: random.h:502
Gaussian distribution.
Definition: random.h:641
unsigned long seed_from_devurandom(void)
Set the seed using the /dev/urandom device.

Generated on Mon Jun 1 2015 12:29:51 for yat by  doxygen 1.8.5