ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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
22 
23  Interface class for Random number generation
24 */
25 
26 #include "Math/RandomFunctions.h"
27 
28 
29 namespace ROOT {
30 namespace Math {
31 
32 
33 //___________________________________________________________________________________
34  /**
35  Documentation for the Random class
36 
37  @ingroup Random
38  */
39 
40  template < class Engine>
41  class Random {
42 
43  public:
44 
45  typedef typename Engine::BaseType EngineBaseType;
47 
48  Random() :
49  fEngine(),
51  {}
52 
53  explicit Random(unsigned int seed) :
54  fEngine(),
56  {
57  fEngine.SetSeed(seed);
58  }
59 
60  double Rndm() {
61  return fEngine();
62  }
63 
64  /**
65  Generate an array of random numbers between ]0,1]
66  0 is excluded and 1 is included
67  Function to preserve ROOT Trandom compatibility
68  */
69  void RndmArray(int n, double * array) {
70  fEngine.RandomArray(array, array+n);
71  }
72 
73  /**
74  Return the type (name) of the used generator
75  */
76  std::string Type() const {
77  return fEngine.Name();
78  }
79 
80  /**
81  Return the size of the generator state
82  */
83  unsigned int EngineSize() const {
84  return fEngine.Size();
85  }
86 
87 
88 
89  double operator() (){
90  return fEngine();
91  }
92 
93  unsigned int Integer() {
94  return fEngine.IntRndm();
95  }
96 
97  Engine & Rng() {
98  return fEngine;
99  }
100 
101  /// Exponential distribution
102  double Exp(double tau) {
103  return fFunctions.Exp(tau);
104  }
105 
106  double Gaus(double mean = 0, double sigma = 1) {
107  return fFunctions.Gaus(mean,sigma);
108  }
109 
110  double Gamma(double a, double b) {
111  return fFunctions.Gamma(a,b);
112  }
113 
114  ///Log-normal distribution
115  double LogNormal(double zeta, double sigma) {
116  return fFunctions.LogNormal(zeta,sigma);
117  }
118 
119  /// chi-square
120  double ChiSquare(double nu) {
121  return fFunctions.ChiSquare(nu);
122  }
123 
124  ///F-distribution
125  double FDist(double nu1, double nu2) {
126  return fFunctions.FDist(nu1,nu2);
127  }
128 
129  /// t student distribution
130  double tDist(double nu) {
131  return fFunctions.tDist(nu);
132  }
133 
134  /// Landau distribution
135  double Landau(double m = 0, double s = 1) {
136  return fFunctions.Landau(m,s);
137  }
138  /// Breit Wigner distribution
139  double BreitWigner(double mean = 0., double gamma = 1) {
140  return fFunctions.BreitWigner(mean,gamma);
141  }
142 
143  /// generate random numbers in a 2D circle of radious 1
144  void Circle(double &x, double &y, double r = 1) {
145  fFunctions.Circle(x,y,r);
146  }
147 
148  /// generate random numbers in a 3D sphere of radious 1
149  void Sphere(double &x, double &y, double &z,double r = 1) {
150  fFunctions.Sphere(x,y,z,r);
151  }
152 
153 
154  ///discrete distributions
155 
156  /// Binomial distribution
157  unsigned int Binomial(unsigned int ntot, double prob) {
158  return fFunctions.Binomial(prob,ntot);
159  }
160 
161 
162  /// Poisson distribution
163  unsigned int Poisson(double mu) {
164  return fFunctions.Poisson(mu);
165  }
166 
167  /// Negative Binomial distribution
168  /// First parameter is n, second is probability
169  /// To be consistent with Random::Binomial
170  unsigned int NegativeBinomial(double n, double prob) {
171  return fFunctions.NegativeBinomial(prob,n);
172  }
173 
174  /// Multinomial distribution
175  std::vector<unsigned int> Multinomial( unsigned int ntot, const std::vector<double> & p ) {
176  return fFunctions.Multinomial(ntot,p);
177  }
178 
179 
180 
181  double Uniform(double a, double b) {
182  return fFunctions.Uniform(a,b);
183  }
184  double Uniform(double a = 1.0) {
185  return fFunctions.Uniform(a);
186  }
187  double Uniform2(double a, double b) {
188  return fFunctions.UniformBase(a,b);
189  }
190 
191 
193  return fFunctions;
194  }
195 
196  void SetSeed(int seed) { fEngine.SetSeed(seed);}
197 
198  private:
199 
200  Engine fEngine;
201  RndmFunctions fFunctions; //! random functions object
202 
203 
204  };
205 
206 
207 
208 
209 } // namespace Math
210 } // namespace ROOT
211 
212 #include "Math/MixMaxEngine.h"
214 
215 namespace ROOT {
216 namespace Math {
217 
218  /// Useful typedef definitions
219 
222 
223 
224 } // namespace Math
225 } // namespace ROOT
226 
227 
228 #endif /* ROOT_Math_Random */
std::vector< unsigned int > Multinomial(unsigned int ntot, const std::vector< double > &p)
Multinomial distribution.
Definition: Random.h:175
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:125
double LogNormal(double, double)
unsigned int Poisson(double mu)
Poisson distribution.
Definition: Random.h:163
double LogNormal(double zeta, double sigma)
Log-normal distribution.
Definition: Random.h:115
void Circle(double &x, double &y, double r=1)
generate random numbers in a 2D circle of radious 1
Definition: Random.h:144
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:45
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:69
TArc * a
Definition: textangle.C:12
double Gamma(double a, double b)
Definition: Random.h:110
Engine fEngine
Definition: Random.h:200
RndmFunctions fFunctions
Definition: Random.h:201
unsigned int Binomial(unsigned int ntot, double prob)
discrete distributions
Definition: Random.h:157
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:46
double Exp(double tau)
Exponential distribution.
Definition: Random.h:102
double Gaus(double mean=0, double sigma=1)
Definition: Random.h:106
double Landau(double m=0, double s=1)
Landau distribution.
Definition: Random.h:135
double BreitWigner(double mean=0., double gamma=1)
Breit Wigner distribution.
Definition: Random.h:139
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:60
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:149
const Double_t sigma
Random(unsigned int seed)
Definition: Random.h:53
double Uniform2(double a, double b)
Definition: Random.h:187
Float_t z[5]
Definition: Ifit.C:16
double FDist(double, double)
double Uniform(double a, double b)
Definition: Random.h:181
double tDist(double nu)
t student distribution
Definition: Random.h:130
double gamma(double x)
ROOT::R::TRInterface & r
Definition: Object.C:4
unsigned int Integer()
Definition: Random.h:93
double ChiSquare(double nu)
chi-square
Definition: Random.h:120
RandomFunctions< Engine, EngineBaseType > & Functions()
Definition: Random.h:192
TMarker * m
Definition: textangle.C:8
void SetSeed(int seed)
Definition: Random.h:196
std::string Type() const
Return the type (name) of the used generator.
Definition: Random.h:76
Engine & Rng()
Definition: Random.h:97
double operator()()
Definition: Random.h:89
Double_t y[n]
Definition: legend1.C:17
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:221
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:170
double Uniform(double a=1.0)
Definition: Random.h:184
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:220
unsigned int EngineSize() const
Return the size of the generator state.
Definition: Random.h:83
Documentation for the Random class.
Definition: Random.h:41