GlobalRng.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief This class subsumes several often used random number generators.
5  *
6  * This class offers convenience functions to generate numbers using a global random number generator from the following distributions:
7  *
8  * <ul>
9  * <li>Bernoulli with name \em coinToss
10  * <li>DiscreteUniform with name \em discrete
11  * <li>Uniform with name \em uni
12  * <li>Normal with name \em gauss
13  * <li>Cauchy with name \em cauchy
14  * <li>Geometric with name \em geom
15  * <li>DiffGeometric with name \em diffGeom
16  * <li>Poisson with name \em poisson
17  * <li>Gamma with name \em gam
18  * <li>Dirichlet with name \em dir
19  * </ul>
20  *
21  * Additionally this class offers a global random number generator. The default
22  * is the Mersenne Twister with a cycle length of $2^19937$. This generator can be used to construct additional
23  * distributions. The seed can be set via Rng::seed .
24  *
25  * \par Example
26  * \code
27  * #include "shark/Rng/GlobalRng.h"
28  *
29  * void main()
30  * {
31  *
32  * // Set seed for all subsumed random number generators:
33  * Rng::seed( 1234 );
34  *
35  * // Get random "numbers" for all subsumed random number generators:
36  * bool rn1 = Rng::coinToss( );
37  * long rn2 = Rng::discrete( );
38  * double rn3 = Rng::uni( );
39  * double rn4 = Rng::gauss( );
40  * double rn5 = Rng::cauchy( );
41  * long rn6 = Rng::geom( );
42  * long rn7 = Rng::diffGeom( );
43  *
44  * // Output of random numbers:
45  * cout << "Bernoulli trial = " << rn1 << endl;
46  * cout << "Discrete distribution number = " << rn2 << endl;
47  * cout << "Uniform distribution number = " << rn3 << endl;
48  * cout << "Normal distribution number = " << rn4 << endl;
49  * cout << "Cauchy distribution number = " << rn5 << endl;
50  * cout << "Geometric distribution number = " << rn6 << endl;
51  * cout << "Differential Geometric distribution number = " << rn7 << endl;
52  * }
53  * \endcode
54  *
55  *
56  *
57  * \author -
58  * \date -
59  *
60  *
61  * \par Copyright 1995-2015 Shark Development Team
62  *
63  * <BR><HR>
64  * This file is part of Shark.
65  * <http://image.diku.dk/shark/>
66  *
67  * Shark is free software: you can redistribute it and/or modify
68  * it under the terms of the GNU Lesser General Public License as published
69  * by the Free Software Foundation, either version 3 of the License, or
70  * (at your option) any later version.
71  *
72  * Shark is distributed in the hope that it will be useful,
73  * but WITHOUT ANY WARRANTY; without even the implied warranty of
74  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
75  * GNU Lesser General Public License for more details.
76  *
77  * You should have received a copy of the GNU Lesser General Public License
78  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
79  *
80  */
81 #ifndef SHARK_RNG_GLOBALRNG_H
82 #define SHARK_RNG_GLOBALRNG_H
83 
84 #include <shark/Rng/Rng.h>
85 
86 #include <shark/Rng/Bernoulli.h>
87 #include <shark/Rng/Binomial.h>
88 #include <shark/Rng/Cauchy.h>
91 #include <shark/Rng/Erlang.h>
92 #include <shark/Rng/Gamma.h>
93 #include <shark/Rng/Geometric.h>
95 #include <shark/Rng/LogNormal.h>
97 #include <shark/Rng/Normal.h>
98 #include <shark/Rng/Poisson.h>
99 #include <shark/Rng/Uniform.h>
100 #include <shark/Rng/Weibull.h>
101 
102 #include <shark/Rng/Entropy.h>
104 
105 #include <boost/random.hpp>
106 #include <vector>
107 
108 namespace shark {
109 
110  /**
111  * \brief Collection of different variate generators for different distributions.
112  *
113  * \tparam RNG The underlying random number generator, needs to model the boost rng concept.
114  */
115  template<typename RNG>
116  class BaseRng {
117  public:
118 
119  typedef RNG rng_type;
121 
122  //! The global random number generator used by all distributions
123  static rng_type globalRng;
124 
125  //! creates a bernoulli distributed number with propability "p"
126  static inline bool coinToss( double p = 0.5 ) {
127  Bernoulli< rng_type > coin(globalRng,p);
128  return coin();
129  }
130 
131  //! creates a discrete uniform distributed number in the range from "min" to "max"
132  static std::size_t discrete(std::size_t min=0,std::size_t max=1) {
133  if(min == max) return min;
134  DiscreteUniform< rng_type > disc(globalRng,min,max);
135  return disc( min, max );
136  }
137 
138 
139  //! creates a uniform distributed number in the range from "min" to "max"
140  static double uni(double min=0.0,double max=1.0) {
141  if(min == max) return min;
142  Uniform< rng_type > uni( globalRng, min, max );
143  return uni();
144  }
145 
146  //! creates a log-normal distributed number with location "location" and scale "scale"
147  static double logNormal(double location=0.0,double scale=1.0) {
148  LogNormal< rng_type > logNormal(globalRng,location,scale);
149  return logNormal();
150  }
151 
152  //! creates a normal distributed number with mean "mean" and variance "sigma"
153  static double gauss(double mean=0.0,double sigma=1.0) {
154  Normal< rng_type > normal(globalRng,mean,sigma);
155  return normal();
156  }
157 
158  //! creates a cauchy distributed number
159  static double cauchy(double median=0.0,double gamma=1.0) {
160  Cauchy< rng_type > cauchy(globalRng,median,gamma);
161  return cauchy();
162  }
163 
164  //! creates a number using the geometric distribution and propability "p"
165  static int geom(double p=0.0) {
166  Geometric< rng_type > rng(globalRng,p);
167  return rng();
168  }
169 
170  //! creates a number using the diff-geometric distribution with mean "mean"
171  static int diffGeom(double mean = 0.5) {
172  DiffGeometric< rng_type > diff(globalRng,mean);
173  return diff();
174  }
175 
176  //! creates a poission distributed number with mean "mean"
177  static double poisson(double mean=0.01) {
178  Poisson< rng_type > poisson(globalRng,mean);
179  return poisson();
180  }
181 
182  //! creates a number using the gamma distribution
183  static double gam(double k,double theta) {
184  Gamma< rng_type > gamma(globalRng,k,theta);
185  return cauchy();
186  }
187 
188  //! Sets the seed for all random number generators to "s".
189  static void seed( typename rng_type::result_type s ) {
190  globalRng.seed( s );
191  }
192  };
193  template<class Rng>
194  typename BaseRng<Rng>::rng_type BaseRng<Rng>::globalRng = typename BaseRng<Rng>::rng_type();
195 
196  #define ANNOUNCE_SHARK_RNG( boost_rng_type, shark_rng_name )\
197  typedef BaseRng< boost_rng_type > shark_rng_name; \
198 
201 }
202 
203 #endif
204 
205 
206