// @(#)root/mathmore:$Id$
// Author: L. Moneta, A. Zsenei   08/2005

 /**********************************************************************
  *                                                                    *
  * Copyright (c) 2004 ROOT Foundation,  CERN/PH-SFT                   *
  *                                                                    *
  * This library is free software; you can redistribute it and/or      *
  * modify it under the terms of the GNU General Public License        *
  * as published by the Free Software Foundation; either version 2     *
  * of the License, or (at your option) any later version.             *
  *                                                                    *
  * This library is distributed in the hope that it will be useful,    *
  * but WITHOUT ANY WARRANTY; without even the implied warranty of     *
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   *
  * General Public License for more details.                           *
  *                                                                    *
  * You should have received a copy of the GNU General Public License  *
  * along with this library (see file COPYING); if not, write          *
  * to the Free Software Foundation, Inc., 59 Temple Place, Suite      *
  * 330, Boston, MA 02111-1307 USA, or contact the author.             *
  *                                                                    *
  **********************************************************************/

// Header file for class GSLRandom
//
// Created by: moneta  at Sun Nov 21 16:26:03 2004
//
// Last update: Sun Nov 21 16:26:03 2004
//
#ifndef ROOT_Math_Random
#define ROOT_Math_Random

#include <string>
#include <vector>

/**
   @defgroup Random Random number generators and distributions
*/



namespace ROOT {
namespace Math {


//_____________________________________________________________________________________
  /**
     User class for MathMore random numbers template on the Engine type.
     The API of this class followed that of the class ROOT::TRandom.
     It must be implemented using as Engine one of the derived classes of
     ROOT::Math::GSLRandomEngine, like ROOT::Math::GSLRngMT

     @ingroup Random

   */
  template < class Engine>
  class Random {

  public:


    /**
       Create a Random generator. Use default engine constructor.
       Engine will  be initialized via Initialize() function in order to
       allocate resources
     */
    Random() {
      fEngine.Initialize();
    }

    /**
       Create a Random generator based using teh default enfing constructor and
       then setting the given seed.
       Engine will  be initialized via Initialize() function in order to
       allocate resources
     */
    explicit Random(unsigned int seed)  {
      fEngine.Initialize();
      fEngine.SetSeed(seed);
    }

    /**
       Create a Random generator based on a provided generic engine.
       Engine will  be initialized via Initialize() function in order to
       allocate resources
     */
    explicit Random(const Engine & e) : fEngine(e) {
      fEngine.Initialize();
    }

    /**
       Destructor: call Terminate() function of engine to free any
       allocated resource
     */
    ~Random() {
      fEngine.Terminate();
    }

    /**
       Generate random numbers between ]0,1]
       0 is excluded and 1 is included
     */
    double Uniform(double x=1.0) {
      return x*fEngine();
    }
    /**
       Generate random numbers between ]0,1]
       0 is excluded and 1 is included
       Function to preserve ROOT Trandom compatibility
     */
   double Rndm() {
      return fEngine();
    }

    /**
       Generate an array of random numbers between ]0,1]
       0 is excluded and 1 is included
       Function to preserve ROOT Trandom compatibility
     */
    void RndmArray(int n, double * array) {
      fEngine.RandomArray(array, array+n);
    }

    /**
       Return the type (name) of the used generator
     */
    std::string Type() const {
      return fEngine.Name();
    }

    /**
       Return the size of the generator state
     */
    unsigned int EngineSize() const {
      return fEngine.Size();
    }

    /**
     set the random generator seed
     */
    void SetSeed(unsigned int seed) {
      return  fEngine.SetSeed(seed);
    }

    /** Random  Distributions
     Use naming and signatures compatible with ROOT TRandom
     **/

    /**
       Gaussian distribution. Default method (use Ziggurat)
     */
    double Gaus(double mean = 0, double sigma = 1) {
      return mean + fEngine.GaussianZig(sigma);
    }

    /**
       Gaussian distribution (Box-Muller method)
     */
    double GausBM(double mean = 0, double sigma = 1) {
      return mean + fEngine.Gaussian(sigma);
    }

    /**
       Gaussian distribution (Ratio Method)
     */
    double GausR(double mean = 0, double sigma = 1) {
      return mean + fEngine.GaussianRatio(sigma);
    }

    /**
       Gaussian Tail distribution
     */
    double GaussianTail(double a, double sigma = 1) {
      return fEngine.GaussianTail(a,sigma);
    }

    /**
       Bivariate Gaussian distribution with correlation
     */
    void Gaussian2D(double sigmaX, double sigmaY, double rho, double &x, double &y) {
      fEngine.Gaussian2D(sigmaX, sigmaY, rho, x, y);
    }

    /**
       Exponential distribution
     */
    double Exp(double tau) {
      return fEngine.Exponential(tau);
    }
    /**
       Breit Wigner distribution
    */
    double BreitWigner(double mean = 0., double gamma = 1) {
      return mean + fEngine.Cauchy( gamma/2.0 );
    }

    /**
       Landau distribution
     */
    double Landau(double mean = 0, double sigma = 1) {
      return mean + sigma*fEngine.Landau();
    }

    /**
       Gamma distribution
     */
    double Gamma(double a, double b) {
      return fEngine.Gamma(a,b);
    }

    /**
       Log Normal distribution
     */
    double LogNormal(double zeta, double sigma) {
      return fEngine.LogNormal(zeta,sigma);
    }

    /**
       Chi square distribution
     */
    double ChiSquare(double nu) {
      return fEngine.ChiSquare(nu);
    }

    /**
       F distrbution
     */
    double FDist(double nu1, double nu2) {
      return fEngine.FDist(nu1,nu2);
    }

    /**
       t student distribution
     */
    double tDist(double nu) {
      return fEngine.tDist(nu);
    }

    /**
       generate random numbers in a 2D circle of radious 1
     */
    void Circle(double &x, double &y, double r = 1) {
      fEngine.Dir2D(x,y);
      x *= r;
      y *= r;
    }

    /**
       generate random numbers in a 3D sphere of radious 1
     */
    void Sphere(double &x, double &y, double &z,double r = 1) {
      fEngine.Dir3D(x,y,z);
      x *= r;
      y *= r;
      z *= r;
    }

    /**
       Poisson distribution
     */
    unsigned int Poisson(double mu) {
      return fEngine.Poisson(mu);
    }

    /**
       Binomial distribution
     */
    unsigned int Binomial(unsigned int ntot, double prob) {
      return fEngine.Binomial(prob,ntot);
    }

    /**
       Negative Binomial distribution
       First parameter is n, second is probability
       To be consistent with Random::Binomial
     */
     unsigned int NegativeBinomial(double n, double prob) {
      return fEngine.NegativeBinomial(prob,n);
    }

    /**
       Multinomial distribution
     */
    std::vector<unsigned int> Multinomial( unsigned int ntot, const std::vector<double> & p ) {
      return fEngine.Multinomial(ntot,p);
    }


  private:

    Engine fEngine;

  };

} // namespace Math
} // namespace ROOT

#ifndef ROOT_Math_GSLRndmEngines
#include "Math/GSLRndmEngines.h"
#endif

namespace ROOT {
namespace Math {


   typedef   Random<ROOT::Math::GSLRngMT>     RandomMT;
   typedef   Random<ROOT::Math::GSLRngTaus>   RandomTaus;
   typedef   Random<ROOT::Math::GSLRngRanLux> RandomRanLux;
   typedef   Random<ROOT::Math::GSLRngGFSR4>  RandomGFSR4;


} // namespace Math
} // namespace ROOT


#endif /* ROOT_Math_Random */



 Random.h:1
 Random.h:2
 Random.h:3
 Random.h:4
 Random.h:5
 Random.h:6
 Random.h:7
 Random.h:8
 Random.h:9
 Random.h:10
 Random.h:11
 Random.h:12
 Random.h:13
 Random.h:14
 Random.h:15
 Random.h:16
 Random.h:17
 Random.h:18
 Random.h:19
 Random.h:20
 Random.h:21
 Random.h:22
 Random.h:23
 Random.h:24
 Random.h:25
 Random.h:26
 Random.h:27
 Random.h:28
 Random.h:29
 Random.h:30
 Random.h:31
 Random.h:32
 Random.h:33
 Random.h:34
 Random.h:35
 Random.h:36
 Random.h:37
 Random.h:38
 Random.h:39
 Random.h:40
 Random.h:41
 Random.h:42
 Random.h:43
 Random.h:44
 Random.h:45
 Random.h:46
 Random.h:47
 Random.h:48
 Random.h:49
 Random.h:50
 Random.h:51
 Random.h:52
 Random.h:53
 Random.h:54
 Random.h:55
 Random.h:56
 Random.h:57
 Random.h:58
 Random.h:59
 Random.h:60
 Random.h:61
 Random.h:62
 Random.h:63
 Random.h:64
 Random.h:65
 Random.h:66
 Random.h:67
 Random.h:68
 Random.h:69
 Random.h:70
 Random.h:71
 Random.h:72
 Random.h:73
 Random.h:74
 Random.h:75
 Random.h:76
 Random.h:77
 Random.h:78
 Random.h:79
 Random.h:80
 Random.h:81
 Random.h:82
 Random.h:83
 Random.h:84
 Random.h:85
 Random.h:86
 Random.h:87
 Random.h:88
 Random.h:89
 Random.h:90
 Random.h:91
 Random.h:92
 Random.h:93
 Random.h:94
 Random.h:95
 Random.h:96
 Random.h:97
 Random.h:98
 Random.h:99
 Random.h:100
 Random.h:101
 Random.h:102
 Random.h:103
 Random.h:104
 Random.h:105
 Random.h:106
 Random.h:107
 Random.h:108
 Random.h:109
 Random.h:110
 Random.h:111
 Random.h:112
 Random.h:113
 Random.h:114
 Random.h:115
 Random.h:116
 Random.h:117
 Random.h:118
 Random.h:119
 Random.h:120
 Random.h:121
 Random.h:122
 Random.h:123
 Random.h:124
 Random.h:125
 Random.h:126
 Random.h:127
 Random.h:128
 Random.h:129
 Random.h:130
 Random.h:131
 Random.h:132
 Random.h:133
 Random.h:134
 Random.h:135
 Random.h:136
 Random.h:137
 Random.h:138
 Random.h:139
 Random.h:140
 Random.h:141
 Random.h:142
 Random.h:143
 Random.h:144
 Random.h:145
 Random.h:146
 Random.h:147
 Random.h:148
 Random.h:149
 Random.h:150
 Random.h:151
 Random.h:152
 Random.h:153
 Random.h:154
 Random.h:155
 Random.h:156
 Random.h:157
 Random.h:158
 Random.h:159
 Random.h:160
 Random.h:161
 Random.h:162
 Random.h:163
 Random.h:164
 Random.h:165
 Random.h:166
 Random.h:167
 Random.h:168
 Random.h:169
 Random.h:170
 Random.h:171
 Random.h:172
 Random.h:173
 Random.h:174
 Random.h:175
 Random.h:176
 Random.h:177
 Random.h:178
 Random.h:179
 Random.h:180
 Random.h:181
 Random.h:182
 Random.h:183
 Random.h:184
 Random.h:185
 Random.h:186
 Random.h:187
 Random.h:188
 Random.h:189
 Random.h:190
 Random.h:191
 Random.h:192
 Random.h:193
 Random.h:194
 Random.h:195
 Random.h:196
 Random.h:197
 Random.h:198
 Random.h:199
 Random.h:200
 Random.h:201
 Random.h:202
 Random.h:203
 Random.h:204
 Random.h:205
 Random.h:206
 Random.h:207
 Random.h:208
 Random.h:209
 Random.h:210
 Random.h:211
 Random.h:212
 Random.h:213
 Random.h:214
 Random.h:215
 Random.h:216
 Random.h:217
 Random.h:218
 Random.h:219
 Random.h:220
 Random.h:221
 Random.h:222
 Random.h:223
 Random.h:224
 Random.h:225
 Random.h:226
 Random.h:227
 Random.h:228
 Random.h:229
 Random.h:230
 Random.h:231
 Random.h:232
 Random.h:233
 Random.h:234
 Random.h:235
 Random.h:236
 Random.h:237
 Random.h:238
 Random.h:239
 Random.h:240
 Random.h:241
 Random.h:242
 Random.h:243
 Random.h:244
 Random.h:245
 Random.h:246
 Random.h:247
 Random.h:248
 Random.h:249
 Random.h:250
 Random.h:251
 Random.h:252
 Random.h:253
 Random.h:254
 Random.h:255
 Random.h:256
 Random.h:257
 Random.h:258
 Random.h:259
 Random.h:260
 Random.h:261
 Random.h:262
 Random.h:263
 Random.h:264
 Random.h:265
 Random.h:266
 Random.h:267
 Random.h:268
 Random.h:269
 Random.h:270
 Random.h:271
 Random.h:272
 Random.h:273
 Random.h:274
 Random.h:275
 Random.h:276
 Random.h:277
 Random.h:278
 Random.h:279
 Random.h:280
 Random.h:281
 Random.h:282
 Random.h:283
 Random.h:284
 Random.h:285
 Random.h:286
 Random.h:287
 Random.h:288
 Random.h:289
 Random.h:290
 Random.h:291
 Random.h:292
 Random.h:293
 Random.h:294
 Random.h:295
 Random.h:296
 Random.h:297
 Random.h:298
 Random.h:299
 Random.h:300
 Random.h:301
 Random.h:302
 Random.h:303
 Random.h:304
 Random.h:305
 Random.h:306
 Random.h:307
 Random.h:308
 Random.h:309
 Random.h:310
 Random.h:311
 Random.h:312
 Random.h:313
 Random.h:314
 Random.h:315
 Random.h:316
 Random.h:317
 Random.h:318
 Random.h:319
 Random.h:320