00001 // @(#)root/mathmore:$Id: GSLRndmEngines.h 37441 2010-12-09 16:27:54Z moneta $ 00002 // Author: L. Moneta, A. Zsenei 08/2005 00003 00004 /********************************************************************** 00005 * * 00006 * Copyright (c) 2004 ROOT Foundation, CERN/PH-SFT * 00007 * * 00008 * This library is free software; you can redistribute it and/or * 00009 * modify it under the terms of the GNU General Public License * 00010 * as published by the Free Software Foundation; either version 2 * 00011 * of the License, or (at your option) any later version. * 00012 * * 00013 * This library is distributed in the hope that it will be useful, * 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00016 * General Public License for more details. * 00017 * * 00018 * You should have received a copy of the GNU General Public License * 00019 * along with this library (see file COPYING); if not, write * 00020 * to the Free Software Foundation, Inc., 59 Temple Place, Suite * 00021 * 330, Boston, MA 02111-1307 USA, or contact the author. * 00022 * * 00023 **********************************************************************/ 00024 00025 // Header file for class GSLRandom 00026 // 00027 // Created by: moneta at Sun Nov 21 16:26:03 2004 00028 // 00029 // Last update: Sun Nov 21 16:26:03 2004 00030 // 00031 #ifndef ROOT_Math_GSLRndmEngines 00032 #define ROOT_Math_GSLRndmEngines 00033 00034 #include <string> 00035 #include <vector> 00036 00037 namespace ROOT { 00038 namespace Math { 00039 00040 00041 class GSLRngWrapper; 00042 00043 //_________________________________________________________________ 00044 /** 00045 GSLRandomEngine 00046 Base class for all GSL random engines, 00047 normally user instantiate the derived classes 00048 which creates internally the generator. 00049 00050 The main GSL generators (see 00051 <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html"> 00052 here</A>) are available as derived classes 00053 In addition to generate uniform numbers it provides method for 00054 generating numbers according to pre-defined distributions 00055 using the GSL functions from 00056 <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-Number-Distributions.html"> 00057 GSL random number distributions</A>. 00058 00059 00060 00061 @ingroup Random 00062 */ 00063 class GSLRandomEngine { 00064 00065 public: 00066 00067 /** 00068 default constructor. No creation of rng is done. 00069 If then Initialize() is called an engine is created 00070 based on default GSL type (MT) 00071 */ 00072 GSLRandomEngine(); 00073 00074 /** 00075 create from an existing rng. 00076 User manage the rng pointer which is then deleted olny by calling Terminate() 00077 */ 00078 GSLRandomEngine( GSLRngWrapper * rng); 00079 00080 /** 00081 initialize the generator 00082 If no rng is present the default one based on Mersenne and Twister is created 00083 */ 00084 void Initialize(); 00085 00086 /** 00087 delete pointer to contained rng 00088 */ 00089 void Terminate(); 00090 00091 /** 00092 call Terminate() 00093 */ 00094 virtual ~GSLRandomEngine(); 00095 00096 /** 00097 Generate a random number between ]0,1] 00098 0 is excluded and 1 is included 00099 */ 00100 double operator() () const; 00101 00102 /** 00103 Generate an integer number between [0,max-1] (including 0 and max-1) 00104 if max is larger than available range of algorithm 00105 an error message is printed and zero is returned 00106 */ 00107 unsigned int RndmInt(unsigned int max) const; 00108 00109 /** 00110 Generate an array of random numbers. 00111 The iterators points to the random numbers 00112 */ 00113 template<class Iterator> 00114 void RandomArray(Iterator begin, Iterator end) const { 00115 for ( Iterator itr = begin; itr != end; ++itr ) { 00116 *itr = this->operator()(); 00117 } 00118 } 00119 00120 /** 00121 Generate an array of random numbers 00122 The iterators points to the random numbers 00123 */ 00124 void RandomArray(double * begin, double * end) const; 00125 00126 /** 00127 return name of generator 00128 */ 00129 std::string Name() const; 00130 00131 /** 00132 return the state size of generator 00133 */ 00134 unsigned int Size() const; 00135 00136 /** 00137 set the random generator seed 00138 */ 00139 void SetSeed(unsigned int seed) const; 00140 00141 00142 /** @name Random Distributions 00143 Implemented using the 00144 <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-Number-Distributions.html"> 00145 GSL Random number Distributions</A> 00146 **/ 00147 //@{ 00148 /** 00149 Gaussian distribution - default method is Box-Muller (polar method) 00150 */ 00151 double Gaussian(double sigma) const; 00152 00153 /** 00154 Gaussian distribution - Ziggurat method 00155 */ 00156 double GaussianZig(double sigma) const; 00157 00158 /** 00159 Gaussian distribution - Ratio method 00160 */ 00161 double GaussianRatio(double sigma) const; 00162 /** 00163 Gaussian Tail distribution 00164 */ 00165 double GaussianTail(double a, double sigma) const; 00166 00167 /** 00168 Bivariate Gaussian distribution with correlation 00169 */ 00170 void Gaussian2D(double sigmaX, double sigmaY, double rho, double &x, double &y) const; 00171 00172 /** 00173 Exponential distribution 00174 */ 00175 double Exponential(double mu) const; 00176 00177 /** 00178 Cauchy distribution 00179 */ 00180 double Cauchy(double a) const; 00181 00182 /** 00183 Landau distribution 00184 */ 00185 double Landau() const; 00186 00187 /** 00188 Gamma distribution 00189 */ 00190 double Gamma(double a, double b) const; 00191 00192 /** 00193 Log Normal distribution 00194 */ 00195 double LogNormal(double zeta, double sigma) const; 00196 00197 /** 00198 Chi square distribution 00199 */ 00200 double ChiSquare(double nu) const; 00201 00202 /** 00203 F distrbution 00204 */ 00205 double FDist(double nu1, double nu2) const; 00206 00207 /** 00208 t student distribution 00209 */ 00210 double tDist(double nu) const; 00211 00212 /** 00213 generate random numbers in a 2D circle of radious 1 00214 */ 00215 void Dir2D(double &x, double &y) const; 00216 00217 /** 00218 generate random numbers in a 3D sphere of radious 1 00219 */ 00220 void Dir3D(double &x, double &y, double &z) const; 00221 00222 /** 00223 Poisson distribution 00224 */ 00225 unsigned int Poisson(double mu) const; 00226 00227 /** 00228 Binomial distribution 00229 */ 00230 unsigned int Binomial(double p, unsigned int n) const; 00231 00232 /** 00233 Negative Binomial distribution 00234 */ 00235 unsigned int NegativeBinomial(double p, double n) const; 00236 00237 /** 00238 Multinomial distribution 00239 */ 00240 std::vector<unsigned int> Multinomial( unsigned int ntot, const std::vector<double> & p ) const; 00241 00242 //@} 00243 00244 00245 00246 protected: 00247 00248 /// internal method used by the derived class to set the type of generators 00249 void SetType(GSLRngWrapper * r) { 00250 fRng = r; 00251 } 00252 00253 private: 00254 00255 GSLRngWrapper * fRng; // pointer to GSL generator wrapper (managed by the class) 00256 mutable unsigned int fCurTime; // current time used to seed the generator 00257 00258 00259 }; 00260 00261 //_____________________________________________________________________________________ 00262 /** 00263 Mersenne-Twister generator 00264 gsl_rng_mt19937 from 00265 <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A> 00266 00267 00268 @ingroup Random 00269 */ 00270 class GSLRngMT : public GSLRandomEngine { 00271 public: 00272 GSLRngMT(); 00273 }; 00274 00275 //_____________________________________________________________________________________ 00276 /** 00277 Old Ranlux generator (James, Luscher) (default luxury level, p = 223) 00278 (This is eequivalent to TRandom1 with default luxury level) 00279 see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A> 00280 00281 @ingroup Random 00282 */ 00283 class GSLRngRanLux : public GSLRandomEngine { 00284 public: 00285 GSLRngRanLux(); 00286 }; 00287 00288 //_____________________________________________________________________________________ 00289 /** 00290 Second generation of Ranlux generator for single precision with luxury level of 1 00291 (It throws away 202 values for every 12 used) 00292 see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A> 00293 00294 @ingroup Random 00295 */ 00296 class GSLRngRanLuxS1 : public GSLRandomEngine { 00297 public: 00298 GSLRngRanLuxS1(); 00299 }; 00300 typedef GSLRngRanLuxS1 GSLRngRanLux1; // for backward compatibility 00301 00302 //_____________________________________________________________________________________ 00303 /** 00304 Second generation of Ranlux generator for Single precision with luxury level of 2 00305 (It throws away 397 value for every 12 used) 00306 see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A> 00307 00308 @ingroup Random 00309 */ 00310 class GSLRngRanLuxS2 : public GSLRandomEngine { 00311 public: 00312 GSLRngRanLuxS2(); 00313 }; 00314 typedef GSLRngRanLuxS2 GSLRngRanLux2; // for backward compatibility 00315 00316 //_____________________________________________________________________________________ 00317 /** 00318 Double precision (48 bits) version of Second generation of Ranlux generator with luxury level of 1 00319 (It throws away 202 value for every 12 used) 00320 see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A> 00321 00322 @ingroup Random 00323 */ 00324 class GSLRngRanLuxD1 : public GSLRandomEngine { 00325 public: 00326 GSLRngRanLuxD1(); 00327 }; 00328 00329 //_____________________________________________________________________________________ 00330 /** 00331 Double precision (48 bits) version of Second generation of Ranlux generator with luxury level of 2 00332 (It throws away 397 value for every 12 used) 00333 see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A> 00334 00335 @ingroup Random 00336 */ 00337 class GSLRngRanLuxD2 : public GSLRandomEngine { 00338 public: 00339 GSLRngRanLuxD2(); 00340 }; 00341 typedef GSLRngRanLuxD2 GSLRngRanLux48; // for backward compatibility 00342 00343 00344 //_____________________________________________________________________________________ 00345 /** 00346 Tausworthe generator by L'Ecuyer 00347 see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A> 00348 00349 @ingroup Random 00350 */ 00351 class GSLRngTaus : public GSLRandomEngine { 00352 public: 00353 GSLRngTaus(); 00354 }; 00355 00356 //_____________________________________________________________________________________ 00357 /** 00358 Lagged Fibonacci generator by Ziff 00359 see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A> 00360 00361 @ingroup Random 00362 */ 00363 class GSLRngGFSR4 : public GSLRandomEngine { 00364 public: 00365 GSLRngGFSR4(); 00366 }; 00367 00368 //_____________________________________________________________________________________ 00369 /** 00370 Combined multiple recursive generator (L'Ecuyer) 00371 see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A> 00372 00373 @ingroup Random 00374 */ 00375 class GSLRngCMRG : public GSLRandomEngine { 00376 public: 00377 GSLRngCMRG(); 00378 }; 00379 00380 //_____________________________________________________________________________________ 00381 /** 00382 5-th order multiple recursive generator (L'Ecuyer, Blouin and Coutre) 00383 see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A> 00384 00385 @ingroup Random 00386 */ 00387 class GSLRngMRG : public GSLRandomEngine { 00388 public: 00389 GSLRngMRG(); 00390 }; 00391 00392 //_____________________________________________________________________________________ 00393 /** 00394 BSD rand() generator 00395 gsl_rmg_rand from 00396 <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Unix-random-number-generators.html">here</A> 00397 00398 @ingroup Random 00399 */ 00400 class GSLRngRand : public GSLRandomEngine { 00401 public: 00402 GSLRngRand(); 00403 }; 00404 00405 //_____________________________________________________________________________________ 00406 /** 00407 RANMAR generator 00408 see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Unix-random-number-generators.html">here</A> 00409 00410 @ingroup Random 00411 */ 00412 class GSLRngRanMar : public GSLRandomEngine { 00413 public: 00414 GSLRngRanMar(); 00415 }; 00416 00417 //_____________________________________________________________________________________ 00418 /** 00419 MINSTD generator (Park and Miller) 00420 see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Unix-random-number-generators.html">here</A> 00421 00422 @ingroup Random 00423 */ 00424 class GSLRngMinStd : public GSLRandomEngine { 00425 public: 00426 GSLRngMinStd(); 00427 }; 00428 00429 00430 00431 00432 } // namespace Math 00433 } // namespace ROOT 00434 00435 00436 #endif /* ROOT_Math_GSLRndmEngines */ 00437