Random.h

Go to the documentation of this file.
00001 // @(#)root/mathmore:$Id: Random.h 37442 2010-12-09 16:36:55Z 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_Random
00032 #define ROOT_Math_Random
00033 
00034 #include <string> 
00035 #include <vector> 
00036 
00037 /**
00038    @defgroup Random Random number generators and distributions
00039 */ 
00040 
00041 
00042 
00043 namespace ROOT {
00044 namespace Math {
00045 
00046 
00047 //_____________________________________________________________________________________
00048   /**
00049      User class for MathMore random numbers template on the Engine type. 
00050      The API of this class followed that of the class ROOT::TRandom. 
00051      It must be implemented using as Engine one of the derived classes of 
00052      ROOT::Math::GSLRandomEngine, like ROOT::Math::GSLRngMT
00053 
00054      @ingroup Random 
00055 
00056    */ 
00057   template < class Engine> 
00058   class Random { 
00059 
00060   public: 
00061 
00062 
00063     /**
00064        Create a Random generator. Use default engine constructor. 
00065        Engine will  be initialized via Initialize() function in order to 
00066        allocate resources
00067      */
00068     Random() {
00069       fEngine.Initialize(); 
00070     } 
00071 
00072     /**
00073        Create a Random generator based using teh default enfing constructor and 
00074        then setting the given seed. 
00075        Engine will  be initialized via Initialize() function in order to 
00076        allocate resources
00077      */
00078     explicit Random(unsigned int seed)  {
00079       fEngine.Initialize(); 
00080       fEngine.SetSeed(seed);
00081     } 
00082 
00083     /**
00084        Create a Random generator based on a provided generic engine.
00085        Engine will  be initialized via Initialize() function in order to 
00086        allocate resources
00087      */
00088     explicit Random(const Engine & e) : fEngine(e) {
00089       fEngine.Initialize(); 
00090     } 
00091 
00092     /**
00093        Destructor: call Terminate() function of engine to free any 
00094        allocated resource
00095      */
00096     ~Random() { 
00097       fEngine.Terminate(); 
00098     }
00099 
00100     /**
00101        Generate random numbers between ]0,1]
00102        0 is excluded and 1 is included
00103      */
00104     double Uniform(double x=1.0) { 
00105       return x*fEngine(); 
00106     }
00107     /** 
00108        Generate random numbers between ]0,1]
00109        0 is excluded and 1 is included
00110        Function to preserve ROOT Trandom compatibility 
00111      */  
00112    double Rndm() { 
00113       return fEngine(); 
00114     }
00115 
00116     /** 
00117        Generate an array of random numbers between ]0,1]
00118        0 is excluded and 1 is included
00119        Function to preserve ROOT Trandom compatibility 
00120      */ 
00121     void RndmArray(int n, double * array) { 
00122       fEngine.RandomArray(array, array+n);
00123     }
00124 
00125     /**
00126        Return the type (name) of the used generator 
00127      */
00128     std::string Type() const { 
00129       return fEngine.Name();
00130     }
00131 
00132     /**
00133        Return the size of the generator state 
00134      */
00135     unsigned int EngineSize() const { 
00136       return fEngine.Size();
00137     }
00138 
00139     /** 
00140         set the random generator seed 
00141      */ 
00142     void SetSeed(unsigned int seed) { 
00143       return  fEngine.SetSeed(seed);
00144     }
00145     
00146     /** Random  Distributions 
00147         Use naming and signatures compatible with ROOT TRandom
00148      **/
00149 
00150     /**
00151        Gaussian distribution. Default method (use Ziggurat)
00152      */
00153     double Gaus(double mean = 0, double sigma = 1) { 
00154       return mean + fEngine.GaussianZig(sigma);
00155     }  
00156 
00157     /**
00158        Gaussian distribution (Box-Muller method)
00159      */
00160     double GausBM(double mean = 0, double sigma = 1) { 
00161       return mean + fEngine.Gaussian(sigma);
00162     }  
00163 
00164     /**
00165        Gaussian distribution (Ratio Method)
00166      */
00167     double GausR(double mean = 0, double sigma = 1) { 
00168       return mean + fEngine.GaussianRatio(sigma);
00169     }  
00170 
00171     /**
00172        Gaussian Tail distribution
00173      */
00174     double GaussianTail(double a, double sigma = 1) { 
00175       return fEngine.GaussianTail(a,sigma);
00176     }
00177   
00178     /**
00179        Bivariate Gaussian distribution with correlation
00180      */
00181     void Gaussian2D(double sigmaX, double sigmaY, double rho, double &x, double &y) { 
00182       fEngine.Gaussian2D(sigmaX, sigmaY, rho, x, y);
00183     }
00184     
00185     /**
00186        Exponential distribution
00187      */
00188     double Exp(double tau) { 
00189       return fEngine.Exponential(tau);
00190     }
00191     /**
00192        Breit Wigner distribution 
00193     */
00194     double BreitWigner(double mean = 0., double gamma = 1) { 
00195       return mean + fEngine.Cauchy( gamma/2.0 );
00196     } 
00197 
00198     /**
00199        Landau distribution
00200      */
00201     double Landau(double mean = 0, double sigma = 1) { 
00202       return mean + sigma*fEngine.Landau();
00203     } 
00204 
00205     /**
00206        Gamma distribution
00207      */
00208     double Gamma(double a, double b) { 
00209       return fEngine.Gamma(a,b);
00210     } 
00211 
00212     /**
00213        Log Normal distribution
00214      */
00215     double LogNormal(double zeta, double sigma) { 
00216       return fEngine.LogNormal(zeta,sigma);
00217     }
00218 
00219     /**
00220        Chi square distribution
00221      */
00222     double ChiSquare(double nu) { 
00223       return fEngine.ChiSquare(nu);
00224     }
00225 
00226     /**
00227        F distrbution
00228      */
00229     double FDist(double nu1, double nu2) { 
00230       return fEngine.FDist(nu1,nu2);
00231     }
00232     
00233     /**
00234        t student distribution
00235      */
00236     double tDist(double nu) { 
00237       return fEngine.tDist(nu);
00238     }
00239 
00240     /**
00241        generate random numbers in a 2D circle of radious 1 
00242      */
00243     void Circle(double &x, double &y, double r = 1) { 
00244       fEngine.Dir2D(x,y);
00245       x *= r;
00246       y *= r;
00247     } 
00248 
00249     /**
00250        generate random numbers in a 3D sphere of radious 1 
00251      */
00252     void Sphere(double &x, double &y, double &z,double r = 1) { 
00253       fEngine.Dir3D(x,y,z);
00254       x *= r;
00255       y *= r;
00256       z *= r;
00257     } 
00258   
00259     /**
00260        Poisson distribution
00261      */
00262     unsigned int Poisson(double mu) { 
00263       return fEngine.Poisson(mu); 
00264     }
00265 
00266     /**
00267        Binomial distribution
00268      */
00269     unsigned int Binomial(unsigned int ntot, double prob) { 
00270       return fEngine.Binomial(prob,ntot);
00271     }
00272 
00273     /**
00274        Negative Binomial distribution
00275        First parameter is n, second is probability
00276        To be consistent with Random::Binomial
00277      */
00278      unsigned int NegativeBinomial(double n, double prob) { 
00279       return fEngine.NegativeBinomial(prob,n);
00280     }
00281 
00282     /**
00283        Multinomial distribution
00284      */
00285     std::vector<unsigned int> Multinomial( unsigned int ntot, const std::vector<double> & p ) { 
00286       return fEngine.Multinomial(ntot,p);
00287     }
00288 
00289 
00290   private: 
00291 
00292     Engine fEngine; 
00293 
00294   }; 
00295 
00296 
00297 } // namespace Math
00298 } // namespace ROOT
00299 
00300 
00301 #endif /* ROOT_Math_Random */
00302 
00303 
00304 

Generated on Tue Jul 5 14:25:37 2011 for ROOT_528-00b_version by  doxygen 1.5.1