Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:22:08

0001 // @(#)root/mathcore:$Id$
0002 // Authors: L. Moneta    8/2015
0003 
0004 /**********************************************************************
0005  *                                                                    *
0006  * Copyright (c) 2015 , ROOT MathLib Team                             *
0007  *                                                                    *
0008  *                                                                    *
0009  **********************************************************************/
0010 
0011 // Header file for random class
0012 //
0013 //
0014 // Created by: Lorenzo Moneta  : Tue 4 Aug 2015
0015 //
0016 //
0017 #ifndef ROOT_Math_Random
0018 #define ROOT_Math_Random
0019 
0020 /**
0021 @defgroup Random Interface classes for Random number generation
0022 */
0023 
0024 #include "Math/RandomFunctions.h"
0025 
0026 #include <string>
0027 #include <vector>
0028 #include <cstdint>
0029 
0030 
0031 namespace ROOT {
0032 namespace Math {
0033 
0034 
0035 //___________________________________________________________________________________
0036    /**
0037        Documentation for the Random class
0038 
0039        @ingroup  Random
0040    */
0041 
0042    template < class Engine>
0043    class Random {
0044 
0045    public:
0046 
0047       typedef typename Engine::BaseType EngineBaseType;
0048       typedef RandomFunctions<Engine, EngineBaseType> RndmFunctions;
0049 
0050       Random() :
0051          fEngine(),
0052          fFunctions(fEngine)
0053       {}
0054 
0055       explicit Random(unsigned int seed) :
0056          fEngine(),
0057          fFunctions(fEngine)
0058       {
0059          fEngine.SetSeed(seed);
0060       }
0061 
0062       double Rndm() {
0063          return fEngine();
0064       }
0065 
0066       /**
0067          Generate an array of random numbers between ]0,1]
0068          0 is excluded and 1 is included
0069          Function to preserve ROOT Trandom compatibility
0070       */
0071       void RndmArray(int n, double * array) {
0072          fEngine.RandomArray(array, array+n);
0073       }
0074 
0075       /**
0076          Return the type (name) of the used generator
0077       */
0078       std::string Type() const {
0079          return fEngine.Name();
0080       }
0081 
0082       /**
0083        Return the size of the generator state
0084      */
0085       unsigned int EngineSize() const {
0086          return fEngine.Size();
0087       }
0088 
0089 
0090       double operator() (){
0091          return fEngine();
0092       }
0093 
0094       uint64_t Integer() {
0095          return fEngine.IntRndm();
0096       }
0097 
0098       static uint64_t MaxInt()  {
0099          return Engine::Max();
0100       }
0101 
0102       Engine & Rng() {
0103          return fEngine;
0104       }
0105 
0106       /// Exponential distribution
0107       double Exp(double tau) {
0108          return fFunctions.Exp(tau);
0109       }
0110 
0111       double Gaus(double mean = 0, double sigma = 1) {
0112          return fFunctions.Gaus(mean,sigma);
0113       }
0114 
0115       /// Gamma distribution
0116       double Gamma(double a, double b) {
0117          return fFunctions.Gamma(a,b);
0118       }
0119 
0120       /// Beta distribution
0121       double Beta(double a, double b) {
0122          return fFunctions.Beta(a,b);
0123       }
0124 
0125       ///Log-normal distribution
0126       double LogNormal(double zeta, double sigma) {
0127          return fFunctions.LogNormal(zeta,sigma);
0128       }
0129 
0130       /// chi-square
0131       double  ChiSquare(double nu) {
0132          return fFunctions.ChiSquare(nu);
0133       }
0134 
0135       /// Rayleigh distribution
0136       double  Rayleigh(double sigma) {
0137          return fFunctions.Rayleigh(sigma);
0138       }
0139 
0140       /// Logistic distribution
0141       double  Logistic(double a) {
0142          return fFunctions.Logistic(a);
0143       }
0144 
0145       /// Pareto distribution
0146       double  Pareto(double a, double b) {
0147          return fFunctions.Pareto(a, b);
0148       }
0149 
0150       ///F-distribution
0151       double FDist(double nu1, double nu2) {
0152          return fFunctions.FDist(nu1,nu2);
0153       }
0154 
0155       ///  t student distribution
0156       double tDist(double nu) {
0157          return fFunctions.tDist(nu);
0158       }
0159 
0160       /// Landau distribution
0161       double Landau(double m = 0, double s = 1) {
0162          return fFunctions.Landau(m,s);
0163       }
0164      ///  Breit Wigner distribution
0165       double BreitWigner(double mean = 0., double gamma = 1) {
0166          return fFunctions.BreitWigner(mean,gamma);
0167       }
0168 
0169      ///  generate random numbers in a 2D circle of radius 1
0170       void Circle(double &x, double &y, double r = 1) {
0171          fFunctions.Circle(x,y,r);
0172       }
0173 
0174       ///  generate random numbers in a 3D sphere of radius 1
0175       void Sphere(double &x, double &y, double &z,double r = 1) {
0176          fFunctions.Sphere(x,y,z,r);
0177       }
0178 
0179 
0180       ///discrete distributions
0181 
0182       /// Binomial distribution
0183       unsigned int Binomial(unsigned int ntot, double prob) {
0184          return fFunctions.Binomial(prob,ntot);
0185       }
0186 
0187 
0188       ///   Poisson distribution
0189       unsigned int Poisson(double mu)  {
0190          return fFunctions.Poisson(mu);
0191       }
0192 
0193       /// Negative Binomial distribution
0194       ///  First parameter is n, second is probability
0195       ///  To be consistent with Random::Binomial
0196      unsigned int NegativeBinomial(double n, double prob) {
0197       return fFunctions.NegativeBinomial(prob,n);
0198      }
0199 
0200       ///     Multinomial distribution
0201       std::vector<unsigned int> Multinomial( unsigned int ntot, const std::vector<double> & p ) {
0202          return fFunctions.Multinomial(ntot,p);
0203       }
0204 
0205 
0206 
0207       double Uniform(double a, double b) {
0208          return fFunctions.Uniform(a,b);
0209       }
0210       double Uniform(double a = 1.0) {
0211          return fFunctions.Uniform(a);
0212       }
0213       double Uniform2(double a, double b) {
0214          return fFunctions.UniformBase(a,b);
0215       }
0216 
0217 
0218       RandomFunctions<Engine,EngineBaseType> & Functions() {
0219          return fFunctions;
0220       }
0221 
0222       void SetSeed(int seed) { fEngine.SetSeed(seed);}
0223 
0224    private:
0225 
0226       Engine fEngine;             ///<  random generator engine
0227       RndmFunctions fFunctions;   ///<! random functions object
0228 
0229 
0230   };
0231 
0232 
0233 
0234 
0235 } // namespace Math
0236 } // namespace ROOT
0237 
0238 #include "Math/MixMaxEngine.h"
0239 #include "Math/MersenneTwisterEngine.h"
0240 #include "Math/StdEngine.h"
0241 
0242 namespace ROOT {
0243 namespace Math {
0244 
0245    /// Useful typedef definitions
0246 
0247    typedef   Random<ROOT::Math::MixMaxEngine<240,0>>            RandomMixMax;
0248    typedef   Random<ROOT::Math::MersenneTwisterEngine>   RandomMT19937;
0249    typedef   Random<ROOT::Math::StdEngine<std::mt19937_64>> RandomMT64;
0250    typedef   Random<ROOT::Math::StdEngine<std::ranlux48>> RandomRanlux48;
0251 
0252 } // namespace Math
0253 } // namespace ROOT
0254 
0255 
0256 #endif /* ROOT_Math_Random */