Logo ROOT   6.08/07
Reference Guide
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;
45 
46  Random() :
47  fEngine(),
49  {}
50 
51  explicit Random(unsigned int seed) :
52  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 
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 
247 
248 } // namespace Math
249 } // namespace ROOT
250 
251 
252 #endif /* ROOT_Math_Random */
std::vector< unsigned int > Multinomial(unsigned int ntot, const std::vector< double > &p)
Multinomial distribution.
Definition: Random.h:197
int Poisson(double mean)
Generates a random integer N according to a Poisson law.
Random< ROOT::Math::StdEngine< std::mt19937_64 > > RandomMT64
Definition: Random.h:245
double FDist(double nu1, double nu2)
F-distribution.
Definition: Random.h:147
double LogNormal(double, double)
unsigned int Poisson(double mu)
Poisson distribution.
Definition: Random.h:185
double LogNormal(double zeta, double sigma)
Log-normal distribution.
Definition: Random.h:122
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
Definition: StringConv.hxx:21
void Circle(double &x, double &y, double r=1)
generate random numbers in a 2D circle of radious 1
Definition: Random.h:166
double Exp(double tau)
Returns an exponential deviate.
uint64_t Integer()
Definition: Random.h:90
double Gamma(double, double)
methods which are only for GSL random generators
Engine::BaseType EngineBaseType
Definition: Random.h:43
void RndmArray(int n, double *array)
Generate an array of random numbers between ]0,1] 0 is excluded and 1 is included Function to preserv...
Definition: Random.h:67
unsigned int EngineSize() const
Return the size of the generator state.
Definition: Random.h:81
Random< ROOT::Math::StdEngine< std::ranlux48 > > RandomRanlux48
Definition: Random.h:246
TArc * a
Definition: textangle.C:12
double Gamma(double a, double b)
Gamma distribution.
Definition: Random.h:112
double Logistic(double a)
Logistic distribution.
Definition: Random.h:137
Engine fEngine
Definition: Random.h:222
RndmFunctions fFunctions
Definition: Random.h:223
unsigned int Binomial(unsigned int ntot, double prob)
discrete distributions
Definition: Random.h:179
void Circle(double &x, double &y, double r)
Generates random vectors, uniformly distributed over a circle of given radius.
double Landau(double mu, double sigma)
Generate a random number following a Landau distribution with location parameter mu and scale paramet...
Double_t x[n]
Definition: legend1.C:17
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t tau
Definition: TRolke.cxx:630
RandomFunctions< Engine, EngineBaseType > RndmFunctions
Definition: Random.h:44
double Exp(double tau)
Exponential distribution.
Definition: Random.h:103
double Gaus(double mean=0, double sigma=1)
Definition: Random.h:107
double Landau(double m=0, double s=1)
Landau distribution.
Definition: Random.h:157
double BreitWigner(double mean=0., double gamma=1)
Breit Wigner distribution.
Definition: Random.h:161
void Sphere(double &x, double &y, double &z, double r)
Generates random vectors, uniformly distributed over the surface of a sphere of given radius...
double Rndm()
Definition: Random.h:58
double Uniform(double a, double b)
generate random numbers following a Uniform distribution in the [a,b] interval
void Sphere(double &x, double &y, double &z, double r=1)
generate random numbers in a 3D sphere of radious 1
Definition: Random.h:171
const Double_t sigma
Random(unsigned int seed)
Definition: Random.h:51
double Uniform2(double a, double b)
Definition: Random.h:209
std::string Type() const
Return the type (name) of the used generator.
Definition: Random.h:74
double Beta(double a, double b)
Beta distribution.
Definition: Random.h:117
double FDist(double, double)
double Uniform(double a, double b)
Definition: Random.h:203
double tDist(double nu)
t student distribution
Definition: Random.h:152
double gamma(double x)
TRandom2 r(17)
double Pareto(double a, double b)
Pareto distribution.
Definition: Random.h:142
double ChiSquare(double nu)
chi-square
Definition: Random.h:127
RandomFunctions< Engine, EngineBaseType > & Functions()
Definition: Random.h:214
Random< ROOT::Math::MixMaxEngine< 240, 0 > > RandomMixMax
Useful typedef definitions.
Definition: Random.h:243
TMarker * m
Definition: textangle.C:8
void SetSeed(int seed)
Definition: Random.h:218
double Beta(double, double)
Engine & Rng()
Definition: Random.h:98
double operator()()
Definition: Random.h:86
double Rayleigh(double sigma)
Rayleigh distribution.
Definition: Random.h:132
Double_t y[n]
Definition: legend1.C:17
Namespace for new Math classes and functions.
double Gaus(double mean, double sigma)
generate Gaussian number using defqault method
you should not use this method at all Int_t Int_t z
Definition: TRolke.cxx:630
int Binomial(int ntot, double prob)
Generate binomial numbers.
unsigned int NegativeBinomial(double, double)
Random< ROOT::Math::MersenneTwisterEngine > RandomMT19937
Definition: Random.h:244
unsigned int NegativeBinomial(double n, double prob)
Negative Binomial distribution First parameter is n, second is probability To be consistent with Rand...
Definition: Random.h:192
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:202
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
double Pareto(double, double)
double Uniform(double a=1.0)
Definition: Random.h:206
static uint64_t MaxInt()
Definition: Random.h:94
const Int_t n
Definition: legend1.C:16
double BreitWigner(double mean, double gamma)
Return a number distributed following a BreitWigner function with mean and gamma. ...
Documentation for the Random class.
Definition: Random.h:39