yat  0.10.4pre
random.h
1 #ifndef _theplu_yat_random_
2 #define _theplu_yat_random_
3 
4 // $Id: random.h 2901 2012-12-13 01:30:24Z peter $
5 
6 /*
7  Copyright (C) 2005, 2006, 2007, 2008 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2009, 2010, 2011, 2012 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 #include <boost/concept_check.hpp>
30 #include <boost/thread.hpp>
31 #include <boost/thread/tss.hpp>
32 
33 #include <gsl/gsl_rng.h>
34 #include <gsl/gsl_randist.h>
35 
36 #include <algorithm>
37 #include <string>
38 #include <vector>
39 
40 namespace theplu {
41 namespace yat {
42 namespace random {
43 
44  //forward declaration
45  class RNG_state;
46 
82  class RNG
83  {
84  public:
85 
103  static RNG* instance(void);
104 
109  unsigned long max(void) const;
110 
115  unsigned long min(void) const;
116 
120  std::string name(void) const;
121 
129  const gsl_rng* rng(void) const;
130 
144  void seed(unsigned long s) const;
145 
154  unsigned long seed_from_devurandom(void);
155 
167  // return int for backward compatibility with yat 0.8
168  int set_state(const RNG_state&);
169 
170  private:
171  RNG(void);
172 
180  RNG(const RNG&);
181 
186  RNG& operator=(const RNG&);
187 
188  virtual ~RNG(void);
189  void rng_alloc(void) const;
190 
191  static RNG* instance_;
192  // holds one gsl_rng per thread. Access through rng(void) so a
193  // gsl_rng is allocated if necessary.
194  mutable boost::thread_specific_ptr<gsl_rng> rng_;
195  mutable unsigned long seed_;
196  // guard needs to be mutable because major mission for it is to protect seed_ against multi-access, and seed_ is mutable...
197  mutable boost::mutex mutex_;
198  };
199 
200 
204  class RNG_state
205  {
206  public:
210  explicit RNG_state(const RNG*);
211 
217  RNG_state(const RNG_state&);
218 
222  ~RNG_state(void);
223 
227  const gsl_rng* rng(void) const;
228 
234  RNG_state& operator=(const RNG_state&);
235 
236  private:
237  gsl_rng* rng_;
238 
239  void clone(const gsl_rng&);
240  };
241 
242 
243  // --------------------- Discrete distribtuions ---------------------
244 
253  class Discrete
254  {
255  public:
261  typedef unsigned long int result_type;
262 
266  Discrete(void);
267 
271  virtual ~Discrete(void);
272 
283  void seed(unsigned long s) const YAT_DEPRECATE;
284 
293  unsigned long seed_from_devurandom(void) YAT_DEPRECATE;
294 
298  virtual result_type operator()(void) const = 0;
299 
300  protected:
303  };
304 
313  {
314  public:
322  Binomial(double p, unsigned int n);
323 
327  unsigned long operator()(void) const;
328  private:
329  double p_;
330  unsigned int n_;
331  };
332 
336  class DiscreteGeneral : public Discrete
337  {
338  public:
344  explicit DiscreteGeneral(const statistics::Histogram& hist);
345 
352 
356  ~DiscreteGeneral(void);
357 
366  unsigned long operator()(void) const;
367 
373  DiscreteGeneral& operator=(const DiscreteGeneral&);
374 
375  private:
376  void free(void);
377  void preproc(void);
378 
379  gsl_ran_discrete_t* gen_;
380  std::vector<double> p_;
381  };
382 
396  : public Discrete,
397  public std::unary_function<unsigned long, unsigned long>
398  {
399  public:
413  explicit DiscreteUniform(unsigned long n=0);
414 
424  unsigned long operator()(void) const;
425 
436  unsigned long operator()(unsigned long n) const;
437 
438  private:
439  unsigned long range_;
440  };
441 
457  class Poisson : public Discrete
458  {
459  public:
465  explicit Poisson(const double m=1);
466 
470  unsigned long operator()(void) const;
471 
478  unsigned long operator()(const double m) const;
479 
480  private:
481  double m_;
482  };
483 
484  // --------------------- Continuous distribtuions ---------------------
485 
492  {
493  public:
499  typedef double result_type;
500 
504  Continuous(void);
505 
509  virtual ~Continuous(void);
510 
521  void seed(unsigned long s) const YAT_DEPRECATE;
522 
531  unsigned long seed_from_devurandom(void) YAT_DEPRECATE;
532 
536  virtual result_type operator()(void) const = 0;
537 
538  protected:
541  };
542 
543  // ContinuousUniform is declared before ContinuousGeneral to avoid
544  // forward declaration
556  {
557  public:
558  double operator()(void) const;
559  };
560 
565  {
566  public:
572  explicit ContinuousGeneral(const statistics::Histogram& hist);
573 
582  double operator()(void) const;
583 
584  private:
585  const DiscreteGeneral discrete_;
586  const statistics::Histogram hist_;
588  };
589 
598  class Exponential : public Continuous
599  {
600  public:
606  explicit Exponential(const double m=1);
607 
611  double operator()(void) const;
612 
619  double operator()(const double m) const;
620 
621  private:
622  double m_;
623  };
624 
638  class Gaussian : public Continuous
639  {
640  public:
647  explicit Gaussian(const double s=1, const double m=0);
648 
652  double operator()(void) const;
653 
660  double operator()(const double s) const;
661 
668  double operator()(const double s, const double m) const;
669 
670  private:
671  double m_;
672  double s_;
673  };
674 
684  template<typename RandomAccessIterator>
685  void random_shuffle(RandomAccessIterator first, RandomAccessIterator last)
686  {
687  typedef RandomAccessIterator rai;
688  BOOST_CONCEPT_ASSERT((boost::Mutable_RandomAccessIterator<rai>));
689  DiscreteUniform rnd;
690  std::random_shuffle(first, last, rnd);
691  }
692 
693 }}} // of namespace random, yat, and theplu
694 
695 #endif

Generated on Mon Nov 11 2013 09:41:44 for yat by  doxygen 1.8.1