Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
Random.h
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Authors: L. Moneta 8/2015
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2015 , ROOT MathLib Team *
7  * *
8  * *
9  **********************************************************************/
10 
11 // Header file for random class
12 //
13 //
14 // Created by: Lorenzo Moneta : Tue 4 Aug 2015
15 //
16 //
17 #ifndef ROOT_Math_Random
18 #define ROOT_Math_Random
19 
20 /**
21 @defgroup Random Interface class for Random number generation
22 */
23 
24 #include "Math/RandomFunctions.h"
25 
26 
27 namespace ROOT {
28 namespace Math {
29 
30 
31 //___________________________________________________________________________________
32  /**
33  Documentation for the Random class
34 
35  @ingroup Random
36  */
37 
38  template < class Engine>
39  class Random {
40 
41  public:
42 
43  typedef typename Engine::BaseType EngineBaseType;
44  typedef RandomFunctions<Engine, EngineBaseType> RndmFunctions;
45 
46  Random() :
47  fEngine(),
48  fFunctions(fEngine)
49  {}
50 
51  explicit Random(unsigned int seed) :
52  fEngine(),
53  fFunctions(fEngine)
54  {
55  fEngine.SetSeed(seed);
56  }
57 
58  double Rndm() {
59  return fEngine();
60  }
61 
62  /**
63  Generate an array of random numbers between ]0,1]
64  0 is excluded and 1 is included
65  Function to preserve ROOT Trandom compatibility
66  */
67  void RndmArray(int n, double * array) {
68  fEngine.RandomArray(array, array+n);
69  }
70 
71  /**
72  Return the type (name) of the used generator
73  */
74  std::string Type() const {
75  return fEngine.Name();
76  }
77 
78  /**
79  Return the size of the generator state
80  */
81  unsigned int EngineSize() const {
82  return fEngine.Size();
83  }
84 
85 
86  double operator() (){
87  return fEngine();
88  }
89 
90  uint64_t Integer() {
91  return fEngine.IntRndm();
92  }
93 
94  static uint64_t MaxInt() {
95  return Engine::Max();
96  }
97 
98  Engine & Rng() {
99  return fEngine;
100  }
101 
102  /// Exponential distribution
103  double Exp(double tau) {
104  return fFunctions.Exp(tau);
105  }
106 
107  double Gaus(double mean = 0, double sigma = 1) {
108  return fFunctions.Gaus(mean,sigma);
109  }
110 
111  /// Gamma distribution
112  double Gamma(double a, double b) {
113  return fFunctions.Gamma(a,b);
114  }
115 
116  /// Beta distribution
117  double Beta(double a, double b) {
118  return fFunctions.Beta(a,b);
119  }
120 
121  ///Log-normal distribution
122  double LogNormal(double zeta, double sigma) {
123  return fFunctions.LogNormal(zeta,sigma);
124  }
125 
126  /// chi-square
127  double ChiSquare(double nu) {
128  return fFunctions.ChiSquare(nu);
129  }
130 
131  /// Rayleigh distribution
132  double Rayleigh(double sigma) {
133  return fFunctions.Rayleigh(sigma);
134  }
135 
136  /// Logistic distribution
137  double Logistic(double a) {
138  return fFunctions.Logistic(a);
139  }
140 
141  /// Pareto distribution
142  double Pareto(double a, double b) {
143  return fFunctions.Pareto(a, b);
144  }
145 
146  ///F-distribution
147  double FDist(double nu1, double nu2) {
148  return fFunctions.FDist(nu1,nu2);
149  }
150 
151  /// t student distribution
152  double tDist(double nu) {
153  return fFunctions.tDist(nu);
154  }
155 
156  /// Landau distribution
157  double Landau(double m = 0, double s = 1) {
158  return fFunctions.Landau(m,s);
159  }
160  /// Breit Wigner distribution
161  double BreitWigner(double mean = 0., double gamma = 1) {
162  return fFunctions.BreitWigner(mean,gamma);
163  }
164 
165  /// generate random numbers in a 2D circle of radious 1
166  void Circle(double &x, double &y, double r = 1) {
167  fFunctions.Circle(x,y,r);
168  }
169 
170  /// generate random numbers in a 3D sphere of radious 1
171  void Sphere(double &x, double &y, double &z,double r = 1) {
172  fFunctions.Sphere(x,y,z,r);
173  }
174 
175 
176  ///discrete distributions
177 
178  /// Binomial distribution
179  unsigned int Binomial(unsigned int ntot, double prob) {
180  return fFunctions.Binomial(prob,ntot);
181  }
182 
183 
184  /// Poisson distribution
185  unsigned int Poisson(double mu) {
186  return fFunctions.Poisson(mu);
187  }
188 
189  /// Negative Binomial distribution
190  /// First parameter is n, second is probability
191  /// To be consistent with Random::Binomial
192  unsigned int NegativeBinomial(double n, double prob) {
193  return fFunctions.NegativeBinomial(prob,n);
194  }
195 
196  /// Multinomial distribution
197  std::vector<unsigned int> Multinomial( unsigned int ntot, const std::vector<double> & p ) {
198  return fFunctions.Multinomial(ntot,p);
199  }
200 
201 
202 
203  double Uniform(double a, double b) {
204  return fFunctions.Uniform(a,b);
205  }
206  double Uniform(double a = 1.0) {
207  return fFunctions.Uniform(a);
208  }
209  double Uniform2(double a, double b) {
210  return fFunctions.UniformBase(a,b);
211  }
212 
213 
214  RandomFunctions<Engine,EngineBaseType> & Functions() {
215  return fFunctions;
216  }
217 
218  void SetSeed(int seed) { fEngine.SetSeed(seed);}
219 
220  private:
221 
222  Engine fEngine; // random generator engine
223  RndmFunctions fFunctions; //! random functions object
224 
225 
226  };
227 
228 
229 
230 
231 } // namespace Math
232 } // namespace ROOT
233 
234 #include "Math/MixMaxEngine.h"
236 #include "Math/StdEngine.h"
237 
238 namespace ROOT {
239 namespace Math {
240 
241  /// Useful typedef definitions
242 
243  typedef Random<ROOT::Math::MixMaxEngine<240,0>> RandomMixMax;
244  typedef Random<ROOT::Math::MersenneTwisterEngine> RandomMT19937;
245  typedef Random<ROOT::Math::StdEngine<std::mt19937_64>> RandomMT64;
246  typedef Random<ROOT::Math::StdEngine<std::ranlux48>> RandomRanlux48;
247 
248 } // namespace Math
249 } // namespace ROOT
250 
251 
252 #endif /* ROOT_Math_Random */