faif
Random.hpp
1 #ifndef FAIF_RANDOM_H
2 #define FAIF_RANDOM_H
3 
4 // the random generators based on boost::random ( mt19937),
5 // the generator is a synchronized (boost::mutex) singleton
6 
7 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
8 //msvc9.0 generuje smieci dla boost/date_time
9 #pragma warning(disable:4244)
10 //msvc9.0 generuje smieci dla boost/random
11 #pragma warning(disable:4512)
12 
13 #endif
14 
15 #include <algorithm>
16 #include <boost/random.hpp>
17 #include <boost/thread/mutex.hpp>
18 
19 namespace faif {
20 
21  // /** macros defined in stdandard library, but here are defined again,
22  // because of Visual Studio incompatibility with standard library.
23  // VC defines macro min and max in windows.h
24  // */
25  // template<typename T> inline const T& min_val(const T& a, const T& b) {
26  // if(b < a) return b;
27  // return a;
28  // }
29 
30  // template<typename T> inline const T& max_val(const T& a, const T& b) {
31  // if(a < b) return b;
32  // return a;
33  // }
34 
35  /** \brief the singleton, synchronized proxy to boost::Random */
37  public:
38 
39  /** the sileton getInstance method.
40  Not using double-check pattern, because it is assumed, that random generator is initialized
41  in single thread
42  */
44  static RandomSingleton singleton;
45  return singleton;
46  }
47  //accessor - mutex, access to generator
48  boost::mutex& getAccess(){ return access_; }
49 
50  //accessor - random generator from boost
51  boost::mt19937& getRng(){ return rng_;}
52  private:
53  //private constructor
54  RandomSingleton() {
55  //init the random generator
56  rng_.seed( static_cast<unsigned int>( time( 0L ) ) );
57  }
58  //noncopyable
60  //noncopyable
61  RandomSingleton& operator=(const RandomSingleton&);
62  //private destructor
63  ~RandomSingleton() { }
64 
65  //mutex, access to generator
66  boost::mutex access_;
67 
68  //random generator from boost
69  boost::mt19937 rng_;
70  };
71 
72 
73  /** \brief the uniform distribution for double, in given range, e.g. <0,1), uses RandomSingleton */
74  class RandomDouble {
75  public:
76  /** \brief the c-tor random variable generator in range <0,1), uniform distribution */
77  explicit RandomDouble()
78  : access_(RandomSingleton::getInstance().getAccess() ),
79  gen_(RandomSingleton::getInstance().getRng(),
80  boost::uniform_real<double>(0.0, 1.0) )
81  { }
82 
83  /** \brief the c-tor random variable generator in range <min,max), uniform distribution */
84  explicit RandomDouble(double min_v, double max_v)
85  : access_(RandomSingleton::getInstance().getAccess() ),
86  gen_(RandomSingleton::getInstance().getRng(),
87  boost::uniform_real<double>( (std::min)(min_v, max_v), (std::max)(min_v,max_v) ) )
88  { }
89 
90 
91  RandomDouble(const RandomDouble& r) : access_(r.access_), gen_(r.gen_) {}
92  ~RandomDouble(){}
93  /** \brief the method to generate the random variable in given range, uniform distribution */
94  double operator()() {
95  boost::mutex::scoped_lock scoped_lock(access_);
96  return gen_();
97  }
98  private:
99  typedef boost::variate_generator<boost::mt19937&, boost::uniform_real<double> > Generator;
100 
101  RandomDouble& operator=(const RandomDouble&); //!< assignment not allowed
102  boost::mutex& access_;
103  Generator gen_;
104  };
105 
106  /** \brief the uniform distribution for int, in range <min,max>, uses RandomSingleton */
107  class RandomInt {
108  public:
109  explicit RandomInt(int min, int max)
110  : access_(RandomSingleton::getInstance().getAccess() ),
111  gen_( RandomSingleton::getInstance().getRng(), boost::uniform_int<int>(min, max) )
112  { }
113 
114  RandomInt(const RandomInt& r) : access_(r.access_), gen_(r.gen_) {}
115  ~RandomInt(){}
116 
117  /** \brief the method to generate the random variable in range <min, max>, uniform distribution */
118  int operator()() {
119  boost::mutex::scoped_lock scoped_lock(access_);
120  return gen_();
121  }
122  private:
123  typedef boost::variate_generator<boost::mt19937&, boost::uniform_int<int> > Generator;
124 
125  RandomInt& operator=(const RandomInt&); //!< assignment not allowed
126  boost::mutex& access_;
127  Generator gen_;
128  };
129 
130  /** \brief the normal distribution for double, for given mean (mi) and standard deviation (sigma),
131  uses RandomSingleton
132  */
133  class RandomNormal {
134  public:
135  /** \brief the c-tor random variable generator, normal distribution */
136  explicit RandomNormal(double mi, double sigma)
137  : access_(RandomSingleton::getInstance().getAccess() ),
138  gen_( RandomSingleton::getInstance().getRng(), boost::normal_distribution<double>(mi, sigma) )
139  {}
140 
141  RandomNormal(const RandomNormal& r) : access_(r.access_), gen_(r.gen_) {}
142  ~RandomNormal(){}
143  /** \brief the method to generate the random variable with normal distribution */
144  double operator()() {
145  boost::mutex::scoped_lock scoped_lock(access_);
146  return gen_();
147  }
148  private:
149  typedef boost::variate_generator<boost::mt19937&, boost::normal_distribution<double> > Generator;
150 
151  RandomDouble& operator=(const RandomNormal&); //!< assignment not allowed
152  boost::mutex& access_;
153  Generator gen_;
154  };
155 
156 
157 } //namespace faif
158 
159 #endif //FAIF_RANDOM_H
Definition: Chain.h:17
double operator()()
the method to generate the random variable in given range, uniform distribution
Definition: Random.hpp:94
RandomDouble(double min_v, double max_v)
the c-tor random variable generator in range <min,max), uniform distribution
Definition: Random.hpp:84
static RandomSingleton & getInstance()
Definition: Random.hpp:43
STL namespace.
RandomNormal(double mi, double sigma)
the c-tor random variable generator, normal distribution
Definition: Random.hpp:136
the uniform distribution for double, in given range, e.g. <0,1), uses RandomSingleton ...
Definition: Random.hpp:74
double operator()()
the method to generate the random variable with normal distribution
Definition: Random.hpp:144
the uniform distribution for int, in range <min,max>, uses RandomSingleton
Definition: Random.hpp:107
the normal distribution for double, for given mean (mi) and standard deviation (sigma), uses RandomSingleton
Definition: Random.hpp:133
int operator()()
the method to generate the random variable in range <min, max>, uniform distribution ...
Definition: Random.hpp:118
the singleton, synchronized proxy to boost::Random
Definition: Random.hpp:36
RandomDouble()
the c-tor random variable generator in range <0,1), uniform distribution
Definition: Random.hpp:77