Logo ROOT   6.16/01
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
25
26
27namespace ROOT {
28namespace 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
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) {
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
238namespace ROOT {
239namespace Math {
240
241 /// Useful typedef definitions
242
247
248} // namespace Math
249} // namespace ROOT
250
251
252#endif /* ROOT_Math_Random */
ROOT::R::TRInterface & r
Definition: Object.C:4
#define b(i)
Definition: RSha256.hxx:100
double Exp(double tau)
Returns an exponential deviate.
double Landau(double mu, double sigma)
Generate a random number following a Landau distribution with location parameter mu and scale paramet...
double BreitWigner(double mean, double gamma)
Return a number distributed following a BreitWigner function with mean and gamma.
double FDist(double, double)
void Circle(double &x, double &y, double r)
Generates random vectors, uniformly distributed over a circle of given radius.
double Uniform(double a, double b)
generate random numbers following a Uniform distribution in the [a,b] interval
double Pareto(double, double)
unsigned int NegativeBinomial(double, double)
double LogNormal(double, double)
void Sphere(double &x, double &y, double &z, double r)
Generates random vectors, uniformly distributed over the surface of a sphere of given radius.
int Binomial(int ntot, double prob)
Generate binomial numbers.
double Beta(double, double)
double Gamma(double, double)
methods which are only for GSL random generators
int Poisson(double mean)
Generates a random integer N according to a Poisson law.
double Gaus(double mean, double sigma)
generate Gaussian number using defqault method
Documentation for the Random class.
Definition: Random.h:39
double Logistic(double a)
Logistic distribution.
Definition: Random.h:137
double Beta(double a, double b)
Beta distribution.
Definition: Random.h:117
Engine fEngine
Definition: Random.h:222
double tDist(double nu)
t student distribution
Definition: Random.h:152
double FDist(double nu1, double nu2)
F-distribution.
Definition: Random.h:147
double ChiSquare(double nu)
chi-square
Definition: Random.h:127
void Circle(double &x, double &y, double r=1)
generate random numbers in a 2D circle of radious 1
Definition: Random.h:166
unsigned int Poisson(double mu)
Poisson distribution.
Definition: Random.h:185
Engine & Rng()
Definition: Random.h:98
double Rayleigh(double sigma)
Rayleigh distribution.
Definition: Random.h:132
static uint64_t MaxInt()
Definition: Random.h:94
double Pareto(double a, double b)
Pareto distribution.
Definition: Random.h:142
Engine::BaseType EngineBaseType
Definition: Random.h:43
std::vector< unsigned int > Multinomial(unsigned int ntot, const std::vector< double > &p)
Multinomial distribution.
Definition: Random.h:197
std::string Type() const
Return the type (name) of the used generator.
Definition: Random.h:74
double Gamma(double a, double b)
Gamma distribution.
Definition: Random.h:112
RandomFunctions< Engine, EngineBaseType > RndmFunctions
Definition: Random.h:44
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 Binomial(unsigned int ntot, double prob)
discrete distributions
Definition: Random.h:179
double LogNormal(double zeta, double sigma)
Log-normal distribution.
Definition: Random.h:122
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
double Exp(double tau)
Exponential distribution.
Definition: Random.h:103
double BreitWigner(double mean=0., double gamma=1)
Breit Wigner distribution.
Definition: Random.h:161
double Uniform(double a, double b)
Definition: Random.h:203
double Uniform(double a=1.0)
Definition: Random.h:206
void SetSeed(int seed)
Definition: Random.h:218
double Landau(double m=0, double s=1)
Landau distribution.
Definition: Random.h:157
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
Random(unsigned int seed)
Definition: Random.h:51
RndmFunctions fFunctions
Definition: Random.h:223
unsigned int EngineSize() const
Return the size of the generator state.
Definition: Random.h:81
double operator()()
Definition: Random.h:86
double Uniform2(double a, double b)
Definition: Random.h:209
double Rndm()
Definition: Random.h:58
RandomFunctions< Engine, EngineBaseType > & Functions()
Definition: Random.h:214
double Gaus(double mean=0, double sigma=1)
Definition: Random.h:107
uint64_t Integer()
Definition: Random.h:90
const Double_t sigma
Double_t y[n]
Definition: legend1.C:17
Double_t x[n]
Definition: legend1.C:17
const Int_t n
Definition: legend1.C:16
Namespace for new Math classes and functions.
double gamma(double x)
Random< ROOT::Math::StdEngine< std::mt19937_64 > > RandomMT64
Definition: Random.h:245
Random< ROOT::Math::StdEngine< std::ranlux48 > > RandomRanlux48
Definition: Random.h:246
Random< ROOT::Math::MixMaxEngine< 240, 0 > > RandomMixMax
Useful typedef definitions.
Definition: Random.h:243
Random< ROOT::Math::MersenneTwisterEngine > RandomMT19937
Definition: Random.h:244
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
static constexpr double s
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:212
auto * m
Definition: textangle.C:8
auto * a
Definition: textangle.C:12