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-2017 Shark Development Team
62  *
63  * <BR><HR>
64  * This file is part of Shark.
65  * <http://shark-ml.org/>
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>
90 #include <shark/Rng/Dirichlet.h>
92 #include <shark/Rng/Erlang.h>
93 #include <shark/Rng/Gamma.h>
94 #include <shark/Rng/Geometric.h>
96 #include <shark/Rng/LogNormal.h>
98 #include <shark/Rng/Normal.h>
99 #include <shark/Rng/Poisson.h>
100 #include <shark/Rng/Uniform.h>
101 #include <shark/Rng/Weibull.h>
102 
103 #include <shark/Rng/Entropy.h>
105 
106 #include <boost/random.hpp>
107 #include <vector>
108 
109 namespace shark {
110 
111  /**
112  * \brief Collection of different variate generators for different distributions.
113  *
114  * \tparam RNG The underlying random number generator, needs to model the boost rng concept.
115  */
116  template<typename RNG>
117  class BaseRng {
118  public:
119 
120  typedef RNG rng_type;
122 
123  //! The global random number generator used by all distributions
124  static rng_type globalRng;
125 
126  //! creates a bernoulli distributed number with propability "p"
127  static inline bool coinToss( double p = 0.5 ) {
128  Bernoulli< rng_type > coin(globalRng,p);
129  return coin();
130  }
131 
132  //! creates a discrete uniform distributed number in the range from "min" to "max"
133  static std::size_t discrete(std::size_t min=0,std::size_t max=1) {
134  if(min == max) return min;
135  DiscreteUniform< rng_type > disc(globalRng,min,max);
136  return disc( min, max );
137  }
138 
139 
140  //! creates a uniform distributed number in the range from "min" to "max"
141  static double uni(double min=0.0,double max=1.0) {
142  if(min == max) return min;
143  Uniform< rng_type > uni( globalRng, min, max );
144  return uni();
145  }
146 
147  //! creates a log-normal distributed number with location "location" and scale "scale"
148  static double logNormal(double location=0.0,double scale=1.0) {
149  LogNormal< rng_type > logNormal(globalRng,location,scale);
150  return logNormal();
151  }
152 
153  //! creates a normal distributed number with mean "mean" and variance "sigma"
154  static double gauss(double mean=0.0,double sigma=1.0) {
155  Normal< rng_type > normal(globalRng,mean,sigma);
156  return normal();
157  }
158 
159  //! creates a cauchy distributed number
160  static double cauchy(double median=0.0,double gamma=1.0) {
161  Cauchy< rng_type > cauchy(globalRng,median,gamma);
162  return cauchy();
163  }
164 
165  //! creates a number using the geometric distribution and propability "p"
166  static int geom(double p=0.0) {
167  Geometric< rng_type > rng(globalRng,p);
168  return rng();
169  }
170 
171  //! creates a number using the diff-geometric distribution with mean "mean"
172  static int diffGeom(double mean = 0.5) {
173  DiffGeometric< rng_type > diff(globalRng,mean);
174  return diff();
175  }
176 
177  //! creates a poission distributed number with mean "mean"
178  static double poisson(double mean=0.01) {
179  Poisson< rng_type > poisson(globalRng,mean);
180  return poisson();
181  }
182 
183  //! creates a number using the gamma distribution
184  static double gam(double k,double theta) {
185  Gamma< rng_type > gamma(globalRng,k,theta);
186  return cauchy();
187  }
188 
189  //! creates a dirichlet distributed number
190  static std::vector<double> dir(size_t n,double alpha) {
191  Dirichlet< rng_type > dist(globalRng,n,alpha);
192  return dist();
193  }
194  //! creates a dirichlet distributed number
195  static std::vector<double> dir(const std::vector<double>& alphas) {
196  Dirichlet< this_type > dist(globalRng,alphas);
197  return dist();
198  }
199 
200  //! Sets the seed for all random number generators to "s".
201  static void seed( typename rng_type::result_type s ) {
202  globalRng.seed( s );
203  }
204  };
205  template<class Rng>
206  typename BaseRng<Rng>::rng_type BaseRng<Rng>::globalRng = typename BaseRng<Rng>::rng_type();
207 
208  #define ANNOUNCE_SHARK_RNG( boost_rng_type, shark_rng_name )\
209  typedef BaseRng< boost_rng_type > shark_rng_name; \
210 
213 }
214 
215 #endif
216 
217 
218