ROOT  6.06/08
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 
87  double operator() (){
88  return fEngine();
89  }
90 
91  unsigned int Integer() {
92  return fEngine.IntRndm();
93  }
94 
95  Engine & Rng() {
96  return fEngine;
97  }
98 
99  /// Exponential distribution
100  double Exp(double tau) {
101  return fFunctions.Exp(tau);
102  }
103 
104  double Gaus(double mean = 0, double sigma = 1) {
105  return fFunctions.Gaus(mean,sigma);
106  }
107 
108  double Gamma(double a, double b) {
109  return fFunctions.Gamma(a,b);
110  }
111 
112  ///Log-normal distribution
113  double LogNormal(double zeta, double sigma) {
114  return fFunctions.LogNormal(zeta,sigma);
115  }
116 
117  /// chi-square
118  double ChiSquare(double nu) {
119  return fFunctions.ChiSquare(nu);
120  }
121 
122  ///F-distribution
123  double FDist(double nu1, double nu2) {
124  return fFunctions.FDist(nu1,nu2);
125  }
126 
127  /// t student distribution
128  double tDist(double nu) {
129  return fFunctions.tDist(nu);
130  }
131 
132  /// Landau distribution
133  double Landau(double m = 0, double s = 1) {
134  return fFunctions.Landau(m,s);
135  }
136  /// Breit Wigner distribution
137  double BreitWigner(double mean = 0., double gamma = 1) {
138  return fFunctions.BreitWigner(mean,gamma);
139  }
140 
141  /// generate random numbers in a 2D circle of radious 1
142  void Circle(double &x, double &y, double r = 1) {
143  fFunctions.Circle(x,y,r);
144  }
145 
146  /// generate random numbers in a 3D sphere of radious 1
147  void Sphere(double &x, double &y, double &z,double r = 1) {
148  fFunctions.Sphere(x,y,z,r);
149  }
150 
151 
152  ///discrete distributions
153 
154  /// Binomial distribution
155  unsigned int Binomial(unsigned int ntot, double prob) {
156  return fFunctions.Binomial(prob,ntot);
157  }
158 
159 
160  /// Poisson distribution
161  unsigned int Poisson(double mu) {
162  return fFunctions.Poisson(mu);
163  }
164 
165  /// Negative Binomial distribution
166  /// First parameter is n, second is probability
167  /// To be consistent with Random::Binomial
168  unsigned int NegativeBinomial(double n, double prob) {
169  return fFunctions.NegativeBinomial(prob,n);
170  }
171 
172  /// Multinomial distribution
173  std::vector<unsigned int> Multinomial( unsigned int ntot, const std::vector<double> & p ) {
174  return fFunctions.Multinomial(ntot,p);
175  }
176 
177 
178 
179  double Uniform(double a, double b) {
180  return fFunctions.Uniform(a,b);
181  }
182  double Uniform(double a = 1.0) {
183  return fFunctions.Uniform(a);
184  }
185  double Uniform2(double a, double b) {
186  return fFunctions.UniformBase(a,b);
187  }
188 
189 
191  return fFunctions;
192  }
193 
194  void SetSeed(int seed) { fEngine.SetSeed(seed);}
195 
196  private:
197 
198  Engine fEngine;
199  RndmFunctions fFunctions; //! random functions object
200 
201 
202  };
203 
204 
205 
206 
207 } // namespace Math
208 } // namespace ROOT
209 
210 #include "Math/MixMaxEngine.h"
212 
213 namespace ROOT {
214 namespace Math {
215 
216  /// Useful typedef definitions
217 
220 
221 
222 } // namespace Math
223 } // namespace ROOT
224 
225 
226 #endif /* ROOT_Math_Random */
std::vector< unsigned int > Multinomial(unsigned int ntot, const std::vector< double > &p)
Multinomial distribution.
Definition: Random.h:173
int Poisson(double mean)
Generates a random integer N according to a Poisson law.
double FDist(double nu1, double nu2)
F-distribution.
Definition: Random.h:123
double LogNormal(double, double)
unsigned int Poisson(double mu)
Poisson distribution.
Definition: Random.h:161
double LogNormal(double zeta, double sigma)
Log-normal distribution.
Definition: Random.h:113
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
void Circle(double &x, double &y, double r=1)
generate random numbers in a 2D circle of radious 1
Definition: Random.h:142
double Exp(double tau)
Returns an exponential deviate.
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
TArc * a
Definition: textangle.C:12
double Gamma(double a, double b)
Definition: Random.h:108
Engine fEngine
Definition: Random.h:198
RndmFunctions fFunctions
Definition: Random.h:199
unsigned int Binomial(unsigned int ntot, double prob)
discrete distributions
Definition: Random.h:155
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
RandomFunctions< Engine, EngineBaseType > RndmFunctions
Definition: Random.h:44
double Exp(double tau)
Exponential distribution.
Definition: Random.h:100
double Gaus(double mean=0, double sigma=1)
Definition: Random.h:104
double Landau(double m=0, double s=1)
Landau distribution.
Definition: Random.h:133
double BreitWigner(double mean=0., double gamma=1)
Breit Wigner distribution.
Definition: Random.h:137
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:147
Random(unsigned int seed)
Definition: Random.h:51
double Uniform2(double a, double b)
Definition: Random.h:185
std::string Type() const
Return the type (name) of the used generator.
Definition: Random.h:74
double FDist(double, double)
double Uniform(double a, double b)
Definition: Random.h:179
double tDist(double nu)
t student distribution
Definition: Random.h:128
double gamma(double x)
ROOT::R::TRInterface & r
Definition: Object.C:4
unsigned int Integer()
Definition: Random.h:91
double ChiSquare(double nu)
chi-square
Definition: Random.h:118
RandomFunctions< Engine, EngineBaseType > & Functions()
Definition: Random.h:190
TMarker * m
Definition: textangle.C:8
void SetSeed(int seed)
Definition: Random.h:194
Engine & Rng()
Definition: Random.h:95
double operator()()
Definition: Random.h:87
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
int Binomial(int ntot, double prob)
Generate binomial numbers.
unsigned int NegativeBinomial(double, double)
Random< ROOT::Math::MersenneTwisterEngine > RandomMT19937
Definition: Random.h:219
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:168
double Uniform(double a=1.0)
Definition: Random.h:182
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. ...
Random< ROOT::Math::MixMaxEngine > RandomMixMax
Useful typedef definitions.
Definition: Random.h:218
Documentation for the Random class.
Definition: Random.h:39