ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GSLRndmEngines.h
Go to the documentation of this file.
1 // @(#)root/mathmore:$Id$
2 // Author: L. Moneta, A. Zsenei 08/2005
3 
4  /**********************************************************************
5  * *
6  * Copyright (c) 2004 ROOT Foundation, CERN/PH-SFT *
7  * *
8  * This library is free software; you can redistribute it and/or *
9  * modify it under the terms of the GNU General Public License *
10  * as published by the Free Software Foundation; either version 2 *
11  * of the License, or (at your option) any later version. *
12  * *
13  * This library is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16  * General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this library (see file COPYING); if not, write *
20  * to the Free Software Foundation, Inc., 59 Temple Place, Suite *
21  * 330, Boston, MA 02111-1307 USA, or contact the author. *
22  * *
23  **********************************************************************/
24 
25 // Header file for class GSLRandom
26 //
27 // Created by: moneta at Sun Nov 21 16:26:03 2004
28 //
29 // Last update: Sun Nov 21 16:26:03 2004
30 //
31 #ifndef ROOT_Math_GSLRndmEngines
32 #define ROOT_Math_GSLRndmEngines
33 
34 #include <string>
35 #include <vector>
36 
37 
38 namespace ROOT {
39 namespace Math {
40 
41 
42  class GSLRngWrapper;
43 
44  //_________________________________________________________________
45  /**
46  GSLRandomEngine
47  Base class for all GSL random engines,
48  normally user instantiate the derived classes
49  which creates internally the generator.
50 
51  The main GSL generators (see
52  <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">
53  here</A>) are available as derived classes
54  In addition to generate uniform numbers it provides method for
55  generating numbers according to pre-defined distributions
56  using the GSL functions from
57  <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-Number-Distributions.html">
58  GSL random number distributions</A>.
59 
60 
61 
62  @ingroup Random
63  */
65 
66  public:
67 
68  /**
69  default constructor. No creation of rng is done.
70  If then Initialize() is called an engine is created
71  based on default GSL type (MT)
72  */
74 
75  /**
76  create from an existing rng.
77  User manage the rng pointer which is then deleted olny by calling Terminate()
78  */
80 
81  /**
82  Copy constructor : clone the contained GSL generator
83  */
84  GSLRandomEngine(const GSLRandomEngine & eng);
85 
86  /**
87  Assignment operator : make a deep copy of the contained GSL generator
88  */
90 
91  /**
92  initialize the generator
93  If no rng is present the default one based on Mersenne and Twister is created
94  */
95  void Initialize();
96 
97  /**
98  delete pointer to contained rng
99  */
100  void Terminate();
101 
102  /**
103  call Terminate()
104  */
105  virtual ~GSLRandomEngine();
106 
107  /**
108  Generate a random number between ]0,1]
109  0 is excluded and 1 is included
110  */
111  double operator() () const;
112 
113  /**
114  Generate a random number between ]0,1]
115  0 is excluded and 1 is included
116  */
117  double Rndm() const { return (*this)(); }
118 
119  /**
120  Generate an integer number between [0,max-1] (including 0 and max-1)
121  if max is larger than available range of algorithm
122  an error message is printed and zero is returned
123  */
124  unsigned int RndmInt(unsigned int max) const;
125 
126  /**
127  Generate an array of random numbers.
128  The iterators points to the random numbers
129  */
130  template<class Iterator>
131  void RandomArray(Iterator begin, Iterator end) const {
132  for ( Iterator itr = begin; itr != end; ++itr ) {
133  *itr = this->operator()();
134  }
135  }
136 
137  /**
138  Generate an array of random numbers
139  The iterators points to the random numbers
140  */
141  void RandomArray(double * begin, double * end) const;
142 
143  /**
144  return name of generator
145  */
146  std::string Name() const;
147 
148  /**
149  return the state size of generator
150  */
151  unsigned int Size() const;
152 
153  /**
154  set the random generator seed
155  */
156  void SetSeed(unsigned int seed) const;
157 
158 
159  /** @name Random Distributions
160  Implemented using the
161  <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-Number-Distributions.html">
162  GSL Random number Distributions</A>
163  **/
164  //@{
165  /**
166  Gaussian distribution - default method is Box-Muller (polar method)
167  */
168  double Gaussian(double sigma) const;
169 
170  /**
171  Gaussian distribution - Ziggurat method
172  */
173  double GaussianZig(double sigma) const;
174 
175  /**
176  Gaussian distribution - Ratio method
177  */
178  double GaussianRatio(double sigma) const;
179  /**
180  Gaussian Tail distribution
181  */
182  double GaussianTail(double a, double sigma) const;
183 
184  /**
185  Bivariate Gaussian distribution with correlation
186  */
187  void Gaussian2D(double sigmaX, double sigmaY, double rho, double &x, double &y) const;
188 
189  /**
190  Exponential distribution
191  */
192  double Exponential(double mu) const;
193 
194  /**
195  Cauchy distribution
196  */
197  double Cauchy(double a) const;
198 
199  /**
200  Landau distribution
201  */
202  double Landau() const;
203 
204  /**
205  Gamma distribution
206  */
207  double Gamma(double a, double b) const;
208 
209  /**
210  Log Normal distribution
211  */
212  double LogNormal(double zeta, double sigma) const;
213 
214  /**
215  Chi square distribution
216  */
217  double ChiSquare(double nu) const;
218 
219  /**
220  F distrbution
221  */
222  double FDist(double nu1, double nu2) const;
223 
224  /**
225  t student distribution
226  */
227  double tDist(double nu) const;
228 
229  /**
230  generate random numbers in a 2D circle of radious 1
231  */
232  void Dir2D(double &x, double &y) const;
233 
234  /**
235  generate random numbers in a 3D sphere of radious 1
236  */
237  void Dir3D(double &x, double &y, double &z) const;
238 
239  /**
240  Poisson distribution
241  */
242  unsigned int Poisson(double mu) const;
243 
244  /**
245  Binomial distribution
246  */
247  unsigned int Binomial(double p, unsigned int n) const;
248 
249  /**
250  Negative Binomial distribution
251  */
252  unsigned int NegativeBinomial(double p, double n) const;
253 
254  /**
255  Multinomial distribution
256  */
257  std::vector<unsigned int> Multinomial( unsigned int ntot, const std::vector<double> & p ) const;
258 
259  //@}
260 
261 
262 
263  protected:
264 
265  /// internal method used by the derived class to set the type of generators
267  fRng = r;
268  }
269 
270  private:
271 
272  GSLRngWrapper * fRng; // pointer to GSL generator wrapper (managed by the class)
273  mutable unsigned int fCurTime; // current time used to seed the generator
274 
275 
276  };
277 
278  //_____________________________________________________________________________________
279  /**
280  Mersenne-Twister generator
281  gsl_rng_mt19937 from
282  <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A>
283 
284 
285  @ingroup Random
286  */
287  class GSLRngMT : public GSLRandomEngine {
288  public:
290  GSLRngMT();
291  };
292 
293  //_____________________________________________________________________________________
294  /**
295  Old Ranlux generator (James, Luscher) (default luxury level, p = 223)
296  (This is eequivalent to TRandom1 with default luxury level)
297  see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A>
298 
299  @ingroup Random
300  */
301  class GSLRngRanLux : public GSLRandomEngine {
302  public:
304  GSLRngRanLux();
305  };
306 
307  //_____________________________________________________________________________________
308  /**
309  Second generation of Ranlux generator for single precision with luxury level of 1
310  (It throws away 202 values for every 12 used)
311  see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A>
312 
313  @ingroup Random
314  */
316  public:
318  GSLRngRanLuxS1();
319  };
320  typedef GSLRngRanLuxS1 GSLRngRanLux1; // for backward compatibility
321 
322  //_____________________________________________________________________________________
323  /**
324  Second generation of Ranlux generator for Single precision with luxury level of 2
325  (It throws away 397 value for every 12 used)
326  see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A>
327 
328  @ingroup Random
329  */
331  public:
333  GSLRngRanLuxS2();
334  };
335  typedef GSLRngRanLuxS2 GSLRngRanLux2; // for backward compatibility
336 
337  //_____________________________________________________________________________________
338  /**
339  Double precision (48 bits) version of Second generation of Ranlux generator with luxury level of 1
340  (It throws away 202 value for every 12 used)
341  see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A>
342 
343  @ingroup Random
344  */
346  public:
348  GSLRngRanLuxD1();
349  };
350 
351  //_____________________________________________________________________________________
352  /**
353  Double precision (48 bits) version of Second generation of Ranlux generator with luxury level of 2
354  (It throws away 397 value for every 12 used)
355  see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A>
356 
357  @ingroup Random
358  */
360  public:
362  GSLRngRanLuxD2();
363  };
364  typedef GSLRngRanLuxD2 GSLRngRanLux48; // for backward compatibility
365 
366 
367  //_____________________________________________________________________________________
368  /**
369  Tausworthe generator by L'Ecuyer
370  see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A>
371 
372  @ingroup Random
373  */
374  class GSLRngTaus : public GSLRandomEngine {
375  public:
377  GSLRngTaus();
378  };
379 
380  //_____________________________________________________________________________________
381  /**
382  Lagged Fibonacci generator by Ziff
383  see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A>
384 
385  @ingroup Random
386  */
387  class GSLRngGFSR4 : public GSLRandomEngine {
388  public:
390  GSLRngGFSR4();
391  };
392 
393  //_____________________________________________________________________________________
394  /**
395  Combined multiple recursive generator (L'Ecuyer)
396  see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A>
397 
398  @ingroup Random
399  */
400  class GSLRngCMRG : public GSLRandomEngine {
401  public:
403  GSLRngCMRG();
404  };
405 
406  //_____________________________________________________________________________________
407  /**
408  5-th order multiple recursive generator (L'Ecuyer, Blouin and Coutre)
409  see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A>
410 
411  @ingroup Random
412  */
413  class GSLRngMRG : public GSLRandomEngine {
414  public:
416  GSLRngMRG();
417  };
418 
419  //_____________________________________________________________________________________
420  /**
421  BSD rand() generator
422  gsl_rmg_rand from
423  <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Unix-random-number-generators.html">here</A>
424 
425  @ingroup Random
426  */
427  class GSLRngRand : public GSLRandomEngine {
428  public:
430  GSLRngRand();
431  };
432 
433  //_____________________________________________________________________________________
434  /**
435  RANMAR generator
436  see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Unix-random-number-generators.html">here</A>
437 
438  @ingroup Random
439  */
440  class GSLRngRanMar : public GSLRandomEngine {
441  public:
443  GSLRngRanMar();
444  };
445 
446  //_____________________________________________________________________________________
447  /**
448  MINSTD generator (Park and Miller)
449  see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Unix-random-number-generators.html">here</A>
450 
451  @ingroup Random
452  */
453  class GSLRngMinStd : public GSLRandomEngine {
454  public:
456  GSLRngMinStd();
457  };
458 
459 
460 
461 
462 } // namespace Math
463 } // namespace ROOT
464 
465 // random functions specialization for GSL
466 // needs to be defined after defining GSLRandomEngine class
467 
468 #include "Math/GSLRandomFunctions.h"
469 
470 #endif /* ROOT_Math_GSLRndmEngines */
471 
double ChiSquare(double nu) const
Chi square distribution.
double GaussianZig(double sigma) const
Gaussian distribution - Ziggurat method.
GSLRandomEngine BaseType
GSLRandomEngine BaseType
Double precision (48 bits) version of Second generation of Ranlux generator with luxury level of 1 (I...
void Gaussian2D(double sigmaX, double sigmaY, double rho, double &x, double &y) const
Bivariate Gaussian distribution with correlation.
GSLRandomEngine & operator=(const GSLRandomEngine &eng)
Assignment operator : make a deep copy of the contained GSL generator.
void SetType(GSLRngWrapper *r)
internal method used by the derived class to set the type of generators
Lagged Fibonacci generator by Ziff see here
double Exponential(double mu) const
Exponential distribution.
GSLRandomEngine BaseType
TArc * a
Definition: textangle.C:12
MINSTD generator (Park and Miller) see here
Double precision (48 bits) version of Second generation of Ranlux generator with luxury level of 2 (I...
GSLRandomEngine BaseType
unsigned int Poisson(double mu) const
Poisson distribution.
double Rndm() const
Generate a random number between ]0,1] 0 is excluded and 1 is included.
Mersenne-Twister generator gsl_rng_mt19937 from here
Double_t x[n]
Definition: legend1.C:17
double Landau() const
Landau distribution.
void Terminate()
delete pointer to contained rng
GSLRandomEngine Base class for all GSL random engines, normally user instantiate the derived classes ...
GSLRandomEngine BaseType
GSLRngWrapper class to wrap gsl_rng structure.
Definition: GSLRngWrapper.h:25
double GaussianRatio(double sigma) const
Gaussian distribution - Ratio method.
GSLRngRanLuxD2 GSLRngRanLux48
const Double_t sigma
Float_t z[5]
Definition: Ifit.C:16
GSLRngRanLuxS2 GSLRngRanLux2
double operator()() const
Generate a random number between ]0,1] 0 is excluded and 1 is included.
unsigned int RndmInt(unsigned int max) const
Generate an integer number between [0,max-1] (including 0 and max-1) if max is larger than available ...
Second generation of Ranlux generator for single precision with luxury level of 1 (It throws away 202...
double FDist(double nu1, double nu2) const
F distrbution.
double Cauchy(double a) const
Cauchy distribution.
virtual ~GSLRandomEngine()
call Terminate()
double Gamma(double a, double b) const
Gamma distribution.
ROOT::R::TRInterface & r
Definition: Object.C:4
unsigned int Binomial(double p, unsigned int n) const
Binomial distribution.
RANMAR generator see here
void RandomArray(Iterator begin, Iterator end) const
Generate an array of random numbers.
Old Ranlux generator (James, Luscher) (default luxury level, p = 223) (This is eequivalent to TRandom...
5-th order multiple recursive generator (L'Ecuyer, Blouin and Coutre) see here
GSLRandomEngine BaseType
std::string Name() const
return name of generator
GSLRandomEngine BaseType
Second generation of Ranlux generator for Single precision with luxury level of 2 (It throws away 397...
Combined multiple recursive generator (L'Ecuyer) see here
GSLRandomEngine BaseType
Double_t y[n]
Definition: legend1.C:17
static Vc_ALWAYS_INLINE int_v max(const int_v &x, const int_v &y)
Definition: vector.h:440
void SetSeed(unsigned int seed) const
set the random generator seed
GSLRngRanLuxS1 GSLRngRanLux1
double LogNormal(double zeta, double sigma) const
Log Normal distribution.
void Dir3D(double &x, double &y, double &z) const
generate random numbers in a 3D sphere of radious 1
unsigned int NegativeBinomial(double p, double n) const
Negative Binomial distribution.
GSLRandomEngine BaseType
Tausworthe generator by L'Ecuyer see here
void Dir2D(double &x, double &y) const
generate random numbers in a 2D circle of radious 1
void Initialize()
initialize the generator If no rng is present the default one based on Mersenne and Twister is create...
double tDist(double nu) const
t student distribution
double Gaussian(double sigma) const
Gaussian distribution - default method is Box-Muller (polar method)
const Int_t n
Definition: legend1.C:16
BSD rand() generator gsl_rmg_rand from here
GSLRandomEngine()
default constructor.
std::vector< unsigned int > Multinomial(unsigned int ntot, const std::vector< double > &p) const
Multinomial distribution.
double GaussianTail(double a, double sigma) const
Gaussian Tail distribution.
unsigned int Size() const
return the state size of generator