ROOT  6.06/09
Reference Guide
GSLRndmEngines.cxx
Go to the documentation of this file.
1 // @(#)root/mathmore:$Id$
2 // Authors: 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 
32 
33 
34 // need to be included later
35 #include <time.h>
36 #include <stdlib.h>
37 #include <cassert>
38 
39 #include "gsl/gsl_rng.h"
40 #include "gsl/gsl_randist.h"
41 
42 
43 #include "Math/GSLRndmEngines.h"
44 #include "GSLRngWrapper.h"
45 
46 extern double gsl_ran_gaussian_acr( const gsl_rng * r, const double sigma);
47 
48 //#include <iostream>
49 
50 namespace ROOT {
51 namespace Math {
52 
53 
54 
55 
56 
57  // default constructor (need to call set type later)
59  fRng(0 ),
60  fCurTime(0)
61  { }
62 
63  // constructor from external rng
64  // internal generator will be managed or not depending on
65  // how the GSLRngWrapper is created
67  fRng(new GSLRngWrapper(*rng) ),
68  fCurTime(0)
69  {}
70 
71  // copy constructor
73  fRng(new GSLRngWrapper(*eng.fRng) ),
74  fCurTime(0)
75  {}
76 
78  // destructor : call terminate if not yet called
79  if (fRng) Terminate();
80  }
81 
82  // assignment operator
84  if (this == &eng) return *this;
85  if (fRng)
86  *fRng = *eng.fRng;
87  else
88  fRng = new GSLRngWrapper(*eng.fRng);
89  fCurTime = eng.fCurTime;
90  return *this;
91  }
92 
93 
95  // initialize the generator by allocating the GSL object
96  // if type was not passed create with default generator
97  if (!fRng) fRng = new GSLRngWrapper();
98  fRng->Allocate();
99  }
100 
102  // terminate the generator by freeing the GSL object
103  if (!fRng) return;
104  fRng->Free();
105  delete fRng;
106  fRng = 0;
107  }
108 
109 
111  // generate random between 0 and 1.
112  // 0 is excluded
113  return gsl_rng_uniform_pos(fRng->Rng() );
114  }
115 
116 
117  unsigned int GSLRandomEngine::RndmInt(unsigned int max) const {
118  // generate a random integer number between 0 and MAX
119  return gsl_rng_uniform_int( fRng->Rng(), max );
120  }
121 
122 // int GSLRandomEngine::GetMin() {
123 // // return minimum integer value used in RndmInt
124 // return gsl_rng_min( fRng->Rng() );
125 // }
126 
127 // int GSLRandomEngine::GetMax() {
128 // // return maximum integr value used in RndmInt
129 // return gsl_rng_max( fRng->Rng() );
130 // }
131 
132  void GSLRandomEngine::RandomArray(double * begin, double * end ) const {
133  // generate array of randoms betweeen 0 and 1. 0 is excluded
134  // specialization for double * (to be faster)
135  for ( double * itr = begin; itr != end; ++itr ) {
136  *itr = gsl_rng_uniform_pos(fRng->Rng() );
137  }
138  }
139 
140  void GSLRandomEngine::SetSeed(unsigned int seed) const {
141  // set the seed, if = 0then the seed is set randomly using an std::rand()
142  // seeded with the current time. Be carefuk in case the current time is
143  // the same in consecutive calls
144  if (seed == 0) {
145  // use like in root (use time)
146  time_t curtime;
147  time(&curtime);
148  unsigned int ct = static_cast<unsigned int>(curtime);
149  if (ct != fCurTime) {
150  fCurTime = ct;
151  // set the seed for rand
152  srand(ct);
153  }
154  seed = rand();
155  }
156 
157  assert(fRng);
158  gsl_rng_set(fRng->Rng(), seed );
159  }
160 
161  std::string GSLRandomEngine::Name() const {
162  //////////////////////////////////////////////////////////////////////////
163 
164  assert ( fRng != 0);
165  assert ( fRng->Rng() != 0 );
166  return std::string( gsl_rng_name( fRng->Rng() ) );
167  }
168 
169  unsigned int GSLRandomEngine::Size() const {
170  //////////////////////////////////////////////////////////////////////////
171 
172  assert (fRng != 0);
173  return gsl_rng_size( fRng->Rng() );
174  }
175 
176 
177  // Random distributions
178 
179  double GSLRandomEngine::GaussianZig(double sigma) const
180  {
181  // Gaussian distribution. Use fast ziggurat algorithm implemented since GSL 1.8
182  return gsl_ran_gaussian_ziggurat( fRng->Rng(), sigma);
183  }
184 
185  double GSLRandomEngine::Gaussian(double sigma) const
186  {
187  // Gaussian distribution. Use default Box-Muller method
188  return gsl_ran_gaussian( fRng->Rng(), sigma);
189  }
190 
191  double GSLRandomEngine::GaussianRatio(double sigma) const
192  {
193  // Gaussian distribution. Use ratio method
194  return gsl_ran_gaussian_ratio_method( fRng->Rng(), sigma);
195  }
196 
197 
198  double GSLRandomEngine::GaussianTail(double a , double sigma) const
199  {
200  // Gaussian Tail distribution: eeturn values larger than a distributed
201  // according to the gaussian
202  return gsl_ran_gaussian_tail( fRng->Rng(), a, sigma);
203  }
204 
205  void GSLRandomEngine::Gaussian2D(double sigmaX, double sigmaY, double rho, double &x, double &y) const
206  {
207  // Gaussian Bivariate distribution, with correlation coefficient rho
208  gsl_ran_bivariate_gaussian( fRng->Rng(), sigmaX, sigmaY, rho, &x, &y);
209  }
210 
211  double GSLRandomEngine::Exponential(double mu) const
212  {
213  // Exponential distribution
214  return gsl_ran_exponential( fRng->Rng(), mu);
215  }
216 
217  double GSLRandomEngine::Cauchy(double a) const
218  {
219  // Cauchy distribution
220  return gsl_ran_cauchy( fRng->Rng(), a);
221  }
222 
223  double GSLRandomEngine::Landau() const
224  {
225  // Landau distribution
226  return gsl_ran_landau( fRng->Rng());
227  }
228 
229  double GSLRandomEngine::Gamma(double a, double b) const
230  {
231  // Gamma distribution
232  return gsl_ran_gamma( fRng->Rng(), a, b);
233  }
234 
235  double GSLRandomEngine::LogNormal(double zeta, double sigma) const
236  {
237  // Log normal distribution
238  return gsl_ran_lognormal( fRng->Rng(), zeta, sigma);
239  }
240 
241  double GSLRandomEngine::ChiSquare(double nu) const
242  {
243  // Chi square distribution
244  return gsl_ran_chisq( fRng->Rng(), nu);
245  }
246 
247 
248  double GSLRandomEngine::FDist(double nu1, double nu2) const
249  {
250  // F distribution
251  return gsl_ran_fdist( fRng->Rng(), nu1, nu2);
252  }
253 
254  double GSLRandomEngine::tDist(double nu) const
255  {
256  // t distribution
257  return gsl_ran_tdist( fRng->Rng(), nu);
258  }
259 
260  void GSLRandomEngine::Dir2D(double &x, double &y) const
261  {
262  // generate random numbers in a 2D circle of radious 1
263  gsl_ran_dir_2d( fRng->Rng(), &x, &y);
264  }
265 
266  void GSLRandomEngine::Dir3D(double &x, double &y, double &z) const
267  {
268  // generate random numbers in a 3D sphere of radious 1
269  gsl_ran_dir_3d( fRng->Rng(), &x, &y, &z);
270  }
271 
272  unsigned int GSLRandomEngine::Poisson(double mu) const
273  {
274  // Poisson distribution
275  return gsl_ran_poisson( fRng->Rng(), mu);
276  }
277 
278  unsigned int GSLRandomEngine::Binomial(double p, unsigned int n) const
279  {
280  // Binomial distribution
281  return gsl_ran_binomial( fRng->Rng(), p, n);
282  }
283 
284  unsigned int GSLRandomEngine::NegativeBinomial(double p, double n) const
285  {
286  // Negative Binomial distribution
287  return gsl_ran_negative_binomial( fRng->Rng(), p, n);
288  }
289 
290 
291  std::vector<unsigned int> GSLRandomEngine::Multinomial( unsigned int ntot, const std::vector<double> & p ) const
292  {
293  // Multinomial distribution return vector of integers which sum is ntot
294  std::vector<unsigned int> ival( p.size());
295  gsl_ran_multinomial( fRng->Rng(), p.size(), ntot, &p.front(), &ival[0]);
296  return ival;
297  }
298 
299 
300 
301  //----------------------------------------------------
302  // generators
303  //----------------------------------------------------
304 
305  /////////////////////////////////////////////////////////////////////////////
306 
308  {
309  SetType(new GSLRngWrapper(gsl_rng_mt19937));
310  Initialize();
311  }
312 
313 
314  // old ranlux - equivalent to TRandom1
316  {
317  SetType(new GSLRngWrapper(gsl_rng_ranlux) );
318  Initialize();
319  }
320 
321  // second generation of Ranlux (single precision version - luxury 1)
323  {
324  SetType(new GSLRngWrapper(gsl_rng_ranlxs1) );
325  Initialize();
326  }
327 
328  // second generation of Ranlux (single precision version - luxury 2)
330  {
331  SetType(new GSLRngWrapper(gsl_rng_ranlxs2) );
332  Initialize();
333  }
334 
335  // double precision version - luxury 1
337  {
338  SetType(new GSLRngWrapper(gsl_rng_ranlxd1) );
339  Initialize();
340  }
341 
342  // double precision version - luxury 2
344  {
345  SetType(new GSLRngWrapper(gsl_rng_ranlxd2) );
346  Initialize();
347  }
348 
349  /////////////////////////////////////////////////////////////////////////////
350 
352  {
353  SetType(new GSLRngWrapper(gsl_rng_taus2) );
354  Initialize();
355  }
356 
357  /////////////////////////////////////////////////////////////////////////////
358 
360  {
361  SetType(new GSLRngWrapper(gsl_rng_gfsr4) );
362  Initialize();
363  }
364 
365  /////////////////////////////////////////////////////////////////////////////
366 
368  {
369  SetType(new GSLRngWrapper(gsl_rng_cmrg) );
370  Initialize();
371  }
372 
373  /////////////////////////////////////////////////////////////////////////////
374 
376  {
377  SetType(new GSLRngWrapper(gsl_rng_mrg) );
378  Initialize();
379  }
380 
381 
382  /////////////////////////////////////////////////////////////////////////////
383 
385  {
386  SetType(new GSLRngWrapper(gsl_rng_rand) );
387  Initialize();
388  }
389 
390  /////////////////////////////////////////////////////////////////////////////
391 
393  {
394  SetType(new GSLRngWrapper(gsl_rng_ranmar) );
395  Initialize();
396  }
397 
398  /////////////////////////////////////////////////////////////////////////////
399 
401  {
402  SetType(new GSLRngWrapper(gsl_rng_minstd) );
403  Initialize();
404  }
405 
406 
407 
408 
409 
410 } // namespace Math
411 } // namespace ROOT
412 
413 
414 
double ChiSquare(double nu) const
Chi square distribution.
double GaussianZig(double sigma) const
Gaussian distribution - Ziggurat method.
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.
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
void SetType(GSLRngWrapper *r)
internal method used by the derived class to set the type of generators
#define assert(cond)
Definition: unittest.h:542
double Exponential(double mu) const
Exponential distribution.
TArc * a
Definition: textangle.C:12
unsigned int Poisson(double mu) const
Poisson distribution.
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 ...
GSLRngWrapper class to wrap gsl_rng structure.
Definition: GSLRngWrapper.h:25
double GaussianRatio(double sigma) const
Gaussian distribution - Ratio method.
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 ...
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.
void RandomArray(Iterator begin, Iterator end) const
Generate an array of random numbers.
std::string Name() const
return name of generator
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
Namespace for new Math classes and functions.
void SetSeed(unsigned int seed) const
set the random generator seed
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.
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
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
double gsl_ran_gaussian_acr(const gsl_rng *r, const double sigma)