Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files 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  class GSLMCIntegrator;
44 
45  //_________________________________________________________________
46  /**
47  GSLRandomEngine
48  Base class for all GSL random engines,
49  normally user instantiate the derived classes
50  which creates internally the generator.
51 
52  The main GSL generators (see
53  <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">
54  here</A>) are available as derived classes
55  In addition to generate uniform numbers it provides method for
56  generating numbers according to pre-defined distributions
57  using the GSL functions from
58  <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-Number-Distributions.html">
59  GSL random number distributions</A>.
60 
61 
62 
63  @ingroup Random
64  */
65  class GSLRandomEngine {
66 
67  friend class GSLMCIntegrator;
68 
69  public:
70 
71  /**
72  default constructor. No creation of rng is done.
73  If then Initialize() is called an engine is created
74  based on default GSL type (MT)
75  */
76  GSLRandomEngine();
77 
78  /**
79  create from an existing rng.
80  User manage the rng pointer which is then deleted olny by calling Terminate()
81  */
82  GSLRandomEngine( GSLRngWrapper * rng);
83 
84  /**
85  Copy constructor : clone the contained GSL generator
86  */
87  GSLRandomEngine(const GSLRandomEngine & eng);
88 
89  /**
90  Assignment operator : make a deep copy of the contained GSL generator
91  */
92  GSLRandomEngine & operator=(const GSLRandomEngine & eng);
93 
94  /**
95  initialize the generator
96  If no rng is present the default one based on Mersenne and Twister is created
97  */
98  void Initialize();
99 
100  /**
101  delete pointer to contained rng
102  */
103  void Terminate();
104 
105  /**
106  call Terminate()
107  */
108  virtual ~GSLRandomEngine();
109 
110  /**
111  Generate a random number between ]0,1]
112  0 is excluded and 1 is included
113  */
114  double operator() () const;
115 
116  /**
117  Generate a random number between ]0,1]
118  0 is excluded and 1 is included
119  */
120  double Rndm() const { return (*this)(); }
121 
122  /**
123  Generate an integer number between [0,max-1] (including 0 and max-1)
124  if max is larger than available range of algorithm
125  an error message is printed and zero is returned
126  */
127  unsigned long RndmInt(unsigned long max) const;
128  /**
129  Generate an integer number between [0,max_generator-1] (including 0 and max-1)
130  if max is larger than available range of algorithm
131  an error message is printed and zero is returned
132  */
133  unsigned long IntRndm() const {
134  return RndmInt(MaxInt()); // max return the largest value the generator can give +1
135  }
136 
137  /**
138  Generate an array of random numbers.
139  The iterators points to the random numbers
140  */
141  template<class Iterator>
142  void RandomArray(Iterator begin, Iterator end) const {
143  for ( Iterator itr = begin; itr != end; ++itr ) {
144  *itr = this->operator()();
145  }
146  }
147 
148  /**
149  Generate an array of random numbers
150  The iterators points to the random numbers
151  */
152  void RandomArray(double * begin, double * end) const;
153 
154  /**
155  return name of generator
156  */
157  std::string Name() const;
158 
159  /**
160  return the state size of generator
161  */
162  unsigned int Size() const;
163 
164  /**
165  return the minimum integer a generator can handle
166  typically this value is 0
167  */
168  unsigned long MinInt() const;
169 
170  /**
171  return the maximum integer +1 a generator can handle
172 
173  */
174  unsigned long MaxInt() const;
175 
176  /**
177  set the random generator seed
178  */
179  void SetSeed(unsigned int seed) const;
180 
181 
182  /** @name Random Distributions
183  Implemented using the
184  <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-Number-Distributions.html">
185  GSL Random number Distributions</A>
186  **/
187  //@{
188  /**
189  Gaussian distribution - default method is Box-Muller (polar method)
190  */
191  double Gaussian(double sigma) const;
192 
193  /**
194  Gaussian distribution - Ziggurat method
195  */
196  double GaussianZig(double sigma) const;
197 
198  /**
199  Gaussian distribution - Ratio method
200  */
201  double GaussianRatio(double sigma) const;
202  /**
203  Gaussian Tail distribution
204  */
205  double GaussianTail(double a, double sigma) const;
206 
207  /**
208  Bivariate Gaussian distribution with correlation
209  */
210  void Gaussian2D(double sigmaX, double sigmaY, double rho, double &x, double &y) const;
211 
212  /**
213  Exponential distribution
214  */
215  double Exponential(double mu) const;
216 
217  /**
218  Cauchy distribution
219  */
220  double Cauchy(double a) const;
221 
222  /**
223  Landau distribution
224  */
225  double Landau() const;
226 
227  /**
228  Gamma distribution
229  */
230  double Gamma(double a, double b) const;
231 
232  /**
233  Beta distribution
234  */
235  double Beta(double a, double b) const;
236 
237  /**
238  Log Normal distribution
239  */
240  double LogNormal(double zeta, double sigma) const;
241 
242  /**
243  Chi square distribution
244  */
245  double ChiSquare(double nu) const;
246 
247  /**
248  F distrbution
249  */
250  double FDist(double nu1, double nu2) const;
251 
252  /**
253  t student distribution
254  */
255  double tDist(double nu) const;
256 
257  /**
258  Rayleigh distribution
259  */
260  double Rayleigh(double sigma) const;
261 
262  /**
263  Logistic distribution
264  */
265  double Logistic(double a) const;
266 
267  /**
268  Pareto distribution
269  */
270  double Pareto(double a, double b) const;
271 
272  /**
273  generate random numbers in a 2D circle of radious 1
274  */
275  void Dir2D(double &x, double &y) const;
276 
277  /**
278  generate random numbers in a 3D sphere of radious 1
279  */
280  void Dir3D(double &x, double &y, double &z) const;
281 
282  /**
283  Poisson distribution
284  */
285  unsigned int Poisson(double mu) const;
286 
287  /**
288  Binomial distribution
289  */
290  unsigned int Binomial(double p, unsigned int n) const;
291 
292  /**
293  Negative Binomial distribution
294  */
295  unsigned int NegativeBinomial(double p, double n) const;
296 
297  /**
298  Multinomial distribution
299  */
300  std::vector<unsigned int> Multinomial( unsigned int ntot, const std::vector<double> & p ) const;
301 
302  //@}
303 
304 
305 
306  protected:
307 
308  /// internal method used by the derived class to set the type of generators
309  void SetType(GSLRngWrapper * r) {
310  fRng = r;
311  }
312 
313  /// internal method to return the engine
314  /// Used by class like GSLMCIntegrator to set the engine
315  GSLRngWrapper * Engine() {
316  return fRng;
317  }
318 
319  private:
320 
321  GSLRngWrapper * fRng; // pointer to GSL generator wrapper (managed by the class)
322  mutable unsigned int fCurTime; // current time used to seed the generator
323 
324 
325  };
326 
327  //_____________________________________________________________________________________
328  /**
329  Mersenne-Twister generator
330  gsl_rng_mt19937 from
331  <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A>
332 
333 
334  @ingroup Random
335  */
336  class GSLRngMT : public GSLRandomEngine {
337  public:
338  typedef GSLRandomEngine BaseType;
339  GSLRngMT();
340  };
341 
342  //_____________________________________________________________________________________
343  /**
344  Old Ranlux generator (James, Luscher) (default luxury level, p = 223)
345  (This is eequivalent to TRandom1 with default luxury level)
346  see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A>
347 
348  @ingroup Random
349  */
350  class GSLRngRanLux : public GSLRandomEngine {
351  public:
352  typedef GSLRandomEngine BaseType;
353  GSLRngRanLux();
354  };
355 
356  //_____________________________________________________________________________________
357  /**
358  Second generation of Ranlux generator for single precision with luxury level of 1
359  (It throws away 202 values for every 12 used)
360  see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A>
361 
362  @ingroup Random
363  */
364  class GSLRngRanLuxS1 : public GSLRandomEngine {
365  public:
366  typedef GSLRandomEngine BaseType;
367  GSLRngRanLuxS1();
368  };
369  typedef GSLRngRanLuxS1 GSLRngRanLux1; // for backward compatibility
370 
371  //_____________________________________________________________________________________
372  /**
373  Second generation of Ranlux generator for Single precision with luxury level of 2
374  (It throws away 397 value for every 12 used)
375  see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A>
376 
377  @ingroup Random
378  */
379  class GSLRngRanLuxS2 : public GSLRandomEngine {
380  public:
381  typedef GSLRandomEngine BaseType;
382  GSLRngRanLuxS2();
383  };
384  typedef GSLRngRanLuxS2 GSLRngRanLux2; // for backward compatibility
385 
386  //_____________________________________________________________________________________
387  /**
388  Double precision (48 bits) version of Second generation of Ranlux generator with luxury level of 1
389  (It throws away 202 value for every 12 used)
390  see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A>
391 
392  @ingroup Random
393  */
394  class GSLRngRanLuxD1 : public GSLRandomEngine {
395  public:
396  typedef GSLRandomEngine BaseType;
397  GSLRngRanLuxD1();
398  };
399 
400  //_____________________________________________________________________________________
401  /**
402  Double precision (48 bits) version of Second generation of Ranlux generator with luxury level of 2
403  (It throws away 397 value for every 12 used)
404  see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A>
405 
406  @ingroup Random
407  */
408  class GSLRngRanLuxD2 : public GSLRandomEngine {
409  public:
410  typedef GSLRandomEngine BaseType;
411  GSLRngRanLuxD2();
412  };
413  typedef GSLRngRanLuxD2 GSLRngRanLux48; // for backward compatibility
414 
415 
416  //_____________________________________________________________________________________
417  /**
418  Tausworthe generator by L'Ecuyer
419  see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A>
420 
421  @ingroup Random
422  */
423  class GSLRngTaus : public GSLRandomEngine {
424  public:
425  typedef GSLRandomEngine BaseType;
426  GSLRngTaus();
427  };
428 
429  //_____________________________________________________________________________________
430  /**
431  Lagged Fibonacci generator by Ziff
432  see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A>
433 
434  @ingroup Random
435  */
436  class GSLRngGFSR4 : public GSLRandomEngine {
437  public:
438  typedef GSLRandomEngine BaseType;
439  GSLRngGFSR4();
440  };
441 
442  //_____________________________________________________________________________________
443  /**
444  Combined multiple recursive generator (L'Ecuyer)
445  see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A>
446 
447  @ingroup Random
448  */
449  class GSLRngCMRG : public GSLRandomEngine {
450  public:
451  typedef GSLRandomEngine BaseType;
452  GSLRngCMRG();
453  };
454 
455  //_____________________________________________________________________________________
456  /**
457  5-th order multiple recursive generator (L'Ecuyer, Blouin and Coutre)
458  see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A>
459 
460  @ingroup Random
461  */
462  class GSLRngMRG : public GSLRandomEngine {
463  public:
464  typedef GSLRandomEngine BaseType;
465  GSLRngMRG();
466  };
467 
468  //_____________________________________________________________________________________
469  /**
470  BSD rand() generator
471  gsl_rmg_rand from
472  <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Unix-random-number-generators.html">here</A>
473 
474  @ingroup Random
475  */
476  class GSLRngRand : public GSLRandomEngine {
477  public:
478  typedef GSLRandomEngine BaseType;
479  GSLRngRand();
480  };
481 
482  //_____________________________________________________________________________________
483  /**
484  RANMAR generator
485  see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Unix-random-number-generators.html">here</A>
486 
487  @ingroup Random
488  */
489  class GSLRngRanMar : public GSLRandomEngine {
490  public:
491  typedef GSLRandomEngine BaseType;
492  GSLRngRanMar();
493  };
494 
495  //_____________________________________________________________________________________
496  /**
497  MINSTD generator (Park and Miller)
498  see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Unix-random-number-generators.html">here</A>
499 
500  @ingroup Random
501  */
502  class GSLRngMinStd : public GSLRandomEngine {
503  public:
504  typedef GSLRandomEngine BaseType;
505  GSLRngMinStd();
506  };
507 
508  /** MixMax generator based on ROOT::Math::MixMaxEngine of N=240
509 
510  @ingroup Random
511  */
512  class GSLRngMixMax : public GSLRandomEngine {
513  public:
514  typedef GSLRandomEngine BaseType;
515  GSLRngMixMax();
516  virtual ~GSLRngMixMax(); // we need a dtcor since is not a standard GSL engine
517  };
518 
519 } // namespace Math
520 } // namespace ROOT
521 
522 // random functions specialization for GSL
523 // needs to be defined after defining GSLRandomEngine class
524 
525 #include "Math/GSLRandomFunctions.h"
526 
527 #endif /* ROOT_Math_GSLRndmEngines */
528