Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RandomFunctions.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_RandomFunctions
18#define ROOT_Math_RandomFunctions
19
20
21#include <type_traits>
22#include <cmath>
23#include "RtypesCore.h"
24#include "TMath.h"
25#include <cassert>
26#include <vector>
27
28#include "TRandomEngine.h"
29
30
31namespace ROOT {
32namespace Math {
33
34
35//___________________________________________________________________________________
36
37
38 // class DefaultEngineType {};
39
40
41 /**
42 Documentation for the RandomFunction class
43
44 @ingroup Random
45 */
46
47
49 //class DefaultEngineType {}; // for generic types
50
51
52
53 /**
54 Definition of the generic implementation class for the RandomFunctions.
55 Needs to have specialized implementations on the different type of engines
56 */
57 template <class EngineBaseType>
59 public:
60 void SetEngine(void *) {}
61 };
62
63 /**
64 Implementation class for the RandomFunction for all the engined that derives from
65 TRandomEngine class, which defines an interface which has TRandomEngine::Rndm()
66 In this way we can have a common implementation for the RandomFunctions
67 */
68
69 template<>
71
72 public:
73
74 /// class constructor
75 RandomFunctionsImpl() : fBaseEngine(nullptr) {}
76
77 void SetEngine(void *r) {
78 fBaseEngine = static_cast<TRandomEngine*>(r);
79 assert(fBaseEngine); // to be sure the static cast works
80 }
81
82
83 ///Generate binomial numbers
84 int Binomial(int ntot, double prob);
85
86 /// Return a number distributed following a BreitWigner function with mean and gamma.
87 double BreitWigner(double mean, double gamma);
88
89 /// Generates random vectors, uniformly distributed over a circle of given radius.
90 /// Input : r = circle radius
91 /// Output: x,y a random 2-d vector of length r
92 void Circle(double &x, double &y, double r);
93
94 /// Returns an exponential deviate.
95 /// exp( -t/tau )
96 double Exp(double tau);
97
98 /// generate Gaussian number using Box-Muller method
99 double GausBM( double mean, double sigma);
100
101 /// generate random numbers according to the Acceptance-Complement-Ratio method
102 double GausACR( double mean, double sigma);
103
104 /// Generate a random number following a Landau distribution
105 /// with location parameter mu and scale parameter sigma:
106 /// Landau( (x-mu)/sigma )
107 double Landau(double mu, double sigma);
108
109 /// Generates a random integer N according to a Poisson law.
110 /// Prob(N) = exp(-mean)*mean^N/Factorial(N)
111 int Poisson(double mean);
112 double PoissonD(double mean);
113
114 /// Generate numbers distributed following a gaussian with mean=0 and sigma=1.
115 /// Using the Box-Muller method
116 void Rannor(double &a, double &b);
117
118 /// Generates random vectors, uniformly distributed over the surface
119 /// of a sphere of given radius.
120 void Sphere(double &x, double &y, double &z, double r);
121
122 /// generate random numbers following a Uniform distribution in the [a,b] interval
123 double Uniform(double a, double b);
124 double Uniform(double a);
125
126 protected:
128
129 private:
130 // Internal method used by the functions
131 double Rndm() { return fBaseEngine->Rndm(); }
132 // for internal usage
133 double Gaus(double mean, double sigma) { return GausACR(mean,sigma); }
134
135
136 };
137
138
139 template < class Engine, class EngineBaseType>
140 class RandomFunctions { //: public RandomFunctionsImpl<EngineBaseType> {
141
142
143 public:
144
145 //RandomFunctions() {}
146
147 RandomFunctions(Engine & rng) : fEngine(&rng) {
148 fImpl.SetEngine(&rng);
149 }
150
151 /// destructor (no op) we do not maintain the engine)
153
154
155 /// non-virtual method
156 inline double operator() () { return (*fEngine)(); }
157
158
159 ///Generate binomial numbers
160 int Binomial(int ntot, double prob) {
161 return fImpl.Binomial(ntot,prob);
162 }
163
164 /// Return a number distributed following a BreitWigner function with mean and gamma.
165 double BreitWigner(double mean, double gamma) {
166 return fImpl.BreitWigner(mean,gamma);
167 }
168
169 /// Generates random vectors, uniformly distributed over a circle of given radius.
170 /// Input : r = circle radius
171 /// Output: x,y a random 2-d vector of length r
172 void Circle(double &x, double &y, double r) {
173 return fImpl.Circle(x,y,r);
174 }
175
176 /// Returns an exponential deviate.
177 /// exp( -t/tau )
178 double Exp(double tau) {
179 return fImpl.Exp(tau);
180 }
181
182 /// generate Gaussian number using Box-Muller method
183 double GausBM( double mean, double sigma) {
184 return fImpl.GausBM(mean,sigma);
185 }
186
187 /// generate random numbers according to the Acceptance-Complement-Ratio method
188 double GausACR( double mean, double sigma) {
189 return fImpl.GausACR(mean, sigma);
190 }
191
192 /// Generate a random number following a Landau distribution
193 /// with location parameter mu and scale parameter sigma:
194 /// Landau( (x-mu)/sigma )
195 double Landau(double mu, double sigma) {
196 return fImpl.Landau(mu,sigma);
197 }
198
199 /// Generates a random integer N according to a Poisson law.
200 /// Prob(N) = exp(-mean)*mean^N/Factorial(N)
201 int Poisson(double mean) { return fImpl.Poisson(mean); }
202 double PoissonD(double mean) { return fImpl.PoissonD(mean); }
203
204 /// Generate numbers distributed following a gaussian with mean=0 and sigma=1.
205 /// Using the Box-Muller method
206 void Rannor(double &a, double &b) {
207 return fImpl.Rannor(a,b);
208 }
209
210 /// Generates random vectors, uniformly distributed over the surface
211 /// of a sphere of given radius.
212 void Sphere(double &x, double &y, double &z, double r) {
213 return fImpl.Sphere(x,y,z,r);
214 }
215
216 /// generate random numbers following a Uniform distribution in the [a,b] interval
217 double Uniform(double a, double b) {
218 return (b-a) * Rndm_impl() + a;
219 }
220
221 /// generate random numbers following a Uniform distribution in the [0,a] interval
222 double Uniform(double a) {
223 return a * Rndm_impl() ;
224 }
225
226
227 /// generate Gaussian number using default method
228 inline double Gaus( double mean, double sigma) {
229 return fImpl.GausACR(mean,sigma);
230 }
231
232
233 // /// re-implement Gaussian
234 // double GausBM2(double mean, double sigma) {
235 // double y = Rndm_impl();
236 // double z = Rndm_impl();
237 // double x = z * 6.28318530717958623;
238 // double radius = std::sqrt(-2*std::log(y));
239 // double g = radius * std::sin(x);
240 // return mean + g * sigma;
241 // }
242
243
244 /// methods which are only for GSL random generators
245
246
247 /// Gamma functions (not implemented here, requires a GSL random engine)
248 double Gamma( double , double ) {
249 //r.Error("Error: Gamma() requires a GSL Engine type");
250 static_assert(std::is_fundamental<Engine>::value,"Error: Gamma() requires a GSL Engine type");
251 return 0;
252 }
253 double Beta( double , double ) {
254 static_assert(std::is_fundamental<Engine>::value,"Error: Beta() requires a GSL Engine type");
255 return 0;
256 }
257 double LogNormal(double, double) {
258 static_assert(std::is_fundamental<Engine>::value,"Error: LogNormal() requires a GSL Engine type");
259 return 0;
260 }
261 double ChiSquare(double) {
262 static_assert(std::is_fundamental<Engine>::value,"Error: ChiSquare() requires a GSL Engine type");
263 return 0;
264 }
265 double Rayleigh( double ) {
266 static_assert(std::is_fundamental<Engine>::value,"Error: Rayleigh() requires a GSL Engine type");
267 return 0;
268 }
269 double Logistic( double ) {
270 static_assert(std::is_fundamental<Engine>::value,"Error: Logistic() requires a GSL Engine type");
271 return 0;
272 }
273 double Pareto( double , double ) {
274 static_assert(std::is_fundamental<Engine>::value,"Error: Pareto() requires a GSL Engine type");
275 return 0;
276 }
277 double FDist(double, double) {
278 static_assert(std::is_fundamental<Engine>::value,"Error: FDist() requires a GSL Engine type");
279 return 0;
280 }
281 double tDist(double) {
282 static_assert(std::is_fundamental<Engine>::value,"Error: tDist() requires a GSL Engine type");
283 return 0;
284 }
285 unsigned int NegativeBinomial(double , double ) {
286 static_assert(std::is_fundamental<Engine>::value,"Error: NegativeBinomial() requires a GSL Engine type");
287 return 0;
288 }
289 std::vector<unsigned int> MultiNomial(unsigned int, const std::vector<double> &){
290 static_assert(std::is_fundamental<Engine>::value,"Error: MultiNomial() requires a GSL Engine type");
291 return std::vector<unsigned int>();
292 }
293
294
295 protected:
296
297 Engine & Rng() { assert(fEngine); return *fEngine; }
298
299 /// Internal implementation to return random number
300 /// Since this one is not a virtual function is faster than Rndm
301 inline double Rndm_impl() { return (*fEngine)(); }
302
303
304 private:
305
306 Engine * fEngine; //! random number generator engine
307 RandomFunctionsImpl<EngineBaseType> fImpl; //! instance of the class implementing the functions
308
309
310 };
311
312
313
314
315} // namespace Math
316} // namespace ROOT
317
318#endif /* ROOT_Math_RandomFunctions */
#define b(i)
Definition RSha256.hxx:100
#define a(i)
Definition RSha256.hxx:99
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
Definition of the generic implementation class for the RandomFunctions.
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 PoissonD(double mean)
double Uniform(double a, double b)
generate random numbers following a Uniform distribution in the [a,b] interval
double GausACR(double mean, double sigma)
generate random numbers according to the Acceptance-Complement-Ratio method
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.
RandomFunctionsImpl< EngineBaseType > fImpl
random number generator engine
double Rndm_impl()
Internal implementation to return random number Since this one is not a virtual function is faster th...
double operator()()
non-virtual method
int Binomial(int ntot, double prob)
Generate binomial numbers.
double Uniform(double a)
generate random numbers following a Uniform distribution in the [0,a] interval
std::vector< unsigned int > MultiNomial(unsigned int, const std::vector< double > &)
double Beta(double, double)
double Gamma(double, double)
methods which are only for GSL random generators
~RandomFunctions()
destructor (no op) we do not maintain the engine)
double GausBM(double mean, double sigma)
generate Gaussian number using Box-Muller method
int Poisson(double mean)
Generates a random integer N according to a Poisson law.
double Gaus(double mean, double sigma)
generate Gaussian number using default method
void Rannor(double &a, double &b)
Generate numbers distributed following a gaussian with mean=0 and sigma=1.
virtual double Rndm()=0
TRandomEngine DefaultEngineType
Documentation for the RandomFunction class.
const Double_t sigma
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
Namespace for new Math classes and functions.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...