thepeg is hosted by Hepforge, IPPP Durham
ThePEG  2.2.1
ThePEG::RandomGenerator Class Referenceabstract

RandomGenerator is an interface to the CLHEP::RandomEngine classes. More...

#include <RandomGenerator.h>

Inheritance diagram for ThePEG::RandomGenerator:

Public Types

typedef vector< double > RndVector
 A vector of doubles. More...
 
typedef RndVector::size_type size_type
 The size_type of RndVector. More...
 
- Public Types inherited from ThePEG::InterfacedBase
enum  InitState { initializing = -1, uninitialized = 0, initialized = 1, runready = 2 }
 Enumeration reflecting the state of an InterfacedBase object. More...
 
- Public Types inherited from ThePEG::Pointer::ReferenceCounted
typedef unsigned int CounterType
 The integer type used for counting.
 

Public Member Functions

virtual void setSeed (long seed)=0
 Reset the underlying random engine with the given seed. More...
 
gsl_rng * getGslInterface ()
 Return a gsl_rng interface to this random generator.
 
Standard constructors and destructors.
 RandomGenerator ()
 Default constructor.
 
 RandomGenerator (const RandomGenerator &)
 Copy-constructor.
 
virtual ~RandomGenerator ()
 Destructor.
 
Functions to return random numbers.
double rnd ()
 Return a (possibly cached) flat random number in the interval $]0,1[$.
 
template<typename Unit >
Unit rnd (Unit b)
 Return a flat random number in the interval $]0,b[$.
 
template<typename Unit >
Unit rnd (Unit a, Unit b)
 Return a flat random number in the interval $]a,b[$.
 
RndVector rndvec (int n)
 Return n flat random number in the interval $]0,1[$.
 
double operator() ()
 Return a (possibly cached) flat random number in the interval $]0,1[$.
 
double operator() (double N)
 Return a (possibly cached) flat integer random number in the interval $[0,N[$. More...
 
long operator() (long N)
 Return a (possibly cached) flat integer random number in the interval $[0,N[$.
 
bool rndbool (double p=0.5)
 Return a true with probability p. More...
 
bool prndbool (double p=0.5)
 Return a true with probability p. More...
 
bool rndbool (double p1, double p2)
 Return a true with probability p1/(p1+p2). More...
 
bool prndbool (double p1, double p2)
 Return a true with probability p1/(p1+p2). More...
 
int rndsign (double p1, double p2, double p3)
 Return -1, 0, or 1 with relative probabilities p1, p2, p3. More...
 
int prndsign (double p1, double p2, double p3)
 Return -1, 0, or 1 with relative probabilities p1, p2, p3. More...
 
int rnd2 (double p0, double p1)
 Return an integer $i$ with probability p $i$/(p0+p1). More...
 
int rnd3 (double p0, double p1, double p2)
 Return an integer $i$ with probability p $i$/(p0+p1+p2). More...
 
int rnd4 (double p0, double p1, double p2, double p3)
 Return an integer/ $i$ with probability p $i$(p0+p1+p2+p3). More...
 
int rnd5 (double p0, double p1, double p2, double p3, double p4)
 Return an integer/ $i$ with probability p $i$(p0+p1+p2+p3+p4). More...
 
double rndExp ()
 Return a number between zero and infinity, distributed according to $e^-x$.
 
template<typename Unit >
Unit rndExp (Unit mean)
 Return a number between zero and infinity, distributed according to $e^-{x/\mu}$ where $\mu$ is the mean value.
 
void rndGaussTwoNumbers (double &randomNumberOne, double &randomNumberTwo)
 Return two numbers distributed according to a Gaussian distribution with zero mean and unit variance. More...
 
double rndGauss ()
 Return a number distributed according to a Gaussian distribution with zero mean and unit variance. More...
 
template<typename Unit >
Unit rndGauss (Unit sigma, Unit mean=Unit())
 Return a number distributed according to a Gaussian distribution with a given standard deviation, sigma, and a given mean.
 
template<typename Unit >
void rndGaussTwoNumbers (Unit &randomNumberOne, Unit &randomNumberTwo, Unit sigma, Unit mean=Unit())
 Return two numbers distributed according to a Gaussian distribution with a given standard deviation, sigma, and a given mean.
 
template<typename Unit >
Unit rndBW (Unit mean, Unit gamma)
 Return a positive number distributed according to a non-relativistic Breit-Wigner with a given width, gamma, and a given mean.
 
template<typename Unit >
Unit rndBW (Unit mean, Unit gamma, Unit cut)
 Return a positive number distributed according to a non-relativistic Breit-Wigner with a given width, gamma, and a given mean. More...
 
template<typename Unit >
Unit rndRelBW (Unit mean, Unit gamma)
 Return a positive number distributed according to a relativistic Breit-Wigner with a given width, gamma, and a given mean.
 
template<typename Unit >
Unit rndRelBW (Unit mean, Unit gamma, Unit cut)
 Return a positive number distributed according to a relativistic Breit-Wigner with a given width, gamma, and a given mean. More...
 
long rndPoisson (double mean)
 Return a non-negative number generated according to a Poissonian distribution with a given mean. More...
 
Access the cached random numbers from the underlying engine.
void push_back (double r)
 Give back a partly unused random number. More...
 
void pop_back ()
 Discard the next random number in the cache.
 
void flush ()
 Discard all random numbers in the cache. More...
 
template<typename OutputIterator >
void rnd (OutputIterator o, size_type n)
 Generate n random numbers between 0 and 1 and put them in the output iterator.
 
Functions used by the persistent I/O system.
void persistentOutput (PersistentOStream &os) const
 Function used to write out object persistently. More...
 
void persistentInput (PersistentIStream &is, int version)
 Function used to read in object persistently. More...
 
- Public Member Functions inherited from ThePEG::Interfaced
virtual ~Interfaced ()
 Empty virtual destructor.
 
virtual bool defaultInit ()
 Functions which are to be used during the actual event generation, after the setup is complete. More...
 
PPtr getParticle (PID) const
 Create a new Particle instance given a id number.
 
PDPtr getParticleData (PID) const
 Return a pointer to the ParticleData object corresponding to the given id number.
 
bool used () const
 Returns true if this object has actally been used.
 
void useMe () const
 Should be called to indicate that this object has actually been used.
 
tEGPtr generator () const
 Return a pointer to the EventGenerator controlling the run. More...
 
void persistentOutput (PersistentOStream &os) const
 Function used to write out object persistently. More...
 
void persistentInput (PersistentIStream &is, int version)
 Function used to read in object persistently. More...
 
- Public Member Functions inherited from ThePEG::InterfacedBase
virtual ~InterfacedBase ()
 The virtual (empty) destructor;.
 
string fullName () const
 Returns the full name of this object including its path, e.g. More...
 
string name () const
 Returns the name of this object, without the path.
 
string path () const
 Returns the path to this object including the trailing '/'. More...
 
string comment () const
 Returns a comment assigned to this object.
 
void setup (istream &is)
 Read setup info from a standard istream is. More...
 
virtual IBPtr fullclone () const
 Return a full clone of this object possibly doing things to the clone to make it sane.
 
virtual void debugme () const
 Print out debugging information for this object on std::cerr. More...
 
void update ()
 Calls the doupdate() function with recursion prevention.
 
void init ()
 Calls the doinit() function with recursion prevention.
 
virtual bool preInitialize () const
 Return true if this object needs to be initialized before all other objects (except those for which this function also returns true). More...
 
void initrun ()
 Calls the doinitrun() function with recursion prevention.
 
void finish ()
 Calls the dofinish() function with recursion prevention.
 
void touch ()
 This function should be called every time something in this object has changed in a way that a sanity check with update() is needed.
 
void reset ()
 Set the state of this object to uninitialized.
 
void clear ()
 Calls reset() and unTouch().
 
InitState state () const
 Return the state of initialization of this object.
 
bool locked () const
 Return true if the BaseRepository is not allowed to change the state of this object.
 
bool touched () const
 Return true if the state of this object has been changed since the last call to update().
 
void persistentOutput (PersistentOStream &os) const
 Function used to write out object persistently. More...
 
void persistentInput (PersistentIStream &is, int version)
 Function used to read in object persistently. More...
 
- Public Member Functions inherited from ThePEG::Base
virtual ~Base ()
 The virtual destructor.
 
void debug () const
 Print out debugging information for this object on std::cerr. More...
 
- Public Member Functions inherited from ThePEG::Pointer::ReferenceCounted
CounterType referenceCount () const
 Return the reference count.
 
- Public Member Functions inherited from ThePEG::Named
 Named (const string &newName=string())
 Constructor with name.
 
 Named (const Named &)=default
 Explicit default copy-constructor (too avoid compiler warnings)
 
const string & name () const
 Return name.
 
bool operator== (const Named &other) const
 Test for equality.
 
bool operator< (const Named &other) const
 Lexicographical comparison.
 

Static Public Member Functions

static void Init ()
 Standard Init function used to initialize the interface.
 
- Static Public Member Functions inherited from ThePEG::Interfaced
static void Init ()
 Standard Init function.
 
- Static Public Member Functions inherited from ThePEG::InterfacedBase
static void Init ()
 Standard Init function.
 
- Static Public Member Functions inherited from ThePEG::Base
static void Init ()
 The standard Init function used to initialize the interfaces. More...
 

Protected Member Functions

virtual void doinit ()
 Initializes this random generator. More...
 
void setSize (size_type newSize)
 Utility function for the interface.
 
virtual void fill ()=0
 Fill the cache with random numbers.
 
- Protected Member Functions inherited from ThePEG::Interfaced
void reporeg (IBPtr object, string name) const
 Register the given object in the Repository with the given name in a subdirectory with the same name as this object. More...
 
template<typename PtrT >
bool setDefaultReference (PtrT &ptr, string classname, string objectname)
 If the pointer, ptr, to an object is not set, create an object of class classname and register it with the Repository with the given objectname in a sib-directory with the same name as this object.
 
 Interfaced ()
 Protected default constructor.
 
 Interfaced (const string &newName)
 Protected constructor taking a name as argument.
 
 Interfaced (const Interfaced &i)
 Protected copy-constructor.
 
void setGenerator (tEGPtr generator)
 Protected function to reset the generator pointer, required for automatic decayer generation in Herwig++ BSM models.
 
- Protected Member Functions inherited from ThePEG::InterfacedBase
virtual IBPtr clone () const =0
 Return a simple clone of this object. More...
 
 InterfacedBase ()
 Protected default constructor.
 
 InterfacedBase (string newName)
 Protected constructor with the name given as argument.
 
 InterfacedBase (const InterfacedBase &i)
 Protected copy-constructor.
 
virtual void readSetup (istream &is)
 Read setup info from a standard istream is. More...
 
virtual void doupdate ()
 Check sanity of the object during the setup phase. More...
 
virtual void doinitrun ()
 Initialize this object. More...
 
virtual void dofinish ()
 Finalize this object. More...
 
virtual IVector getReferences ()
 Return a vector of all pointers to Interfaced objects used in this object. More...
 
virtual void rebind (const TranslationMap &)
 Rebind pointer to other Interfaced objects. More...
 
- Protected Member Functions inherited from ThePEG::Pointer::ReferenceCounted
 ReferenceCounted ()
 Default constructor.
 
 ReferenceCounted (const ReferenceCounted &)
 Copy-constructor.
 
ReferenceCountedoperator= (const ReferenceCounted &)
 Assignment.
 
- Protected Member Functions inherited from ThePEG::Named
const Namedoperator= (const Named &other)
 Assignment.
 
const string & name (const string &newName)
 Set new name.
 

Protected Attributes

RndVector theNumbers
 A vector of cached numbers.
 
RndVector::iterator nextNumber
 Iterator pointing to the next number to be extracted.
 
size_type theSize
 The size of the cache.
 
long theSeed
 The seed to initialize the random generator with.
 
double savedGauss
 A saved Gaussian random number.
 
bool gaussSaved
 Indicate the precense of a saved Gaussian random number.
 
gsl_rng * gsl
 A pinter to a gsl_rng interface to this generator.
 

Private Member Functions

RandomGeneratoroperator= (const RandomGenerator &)=delete
 Private and non-existent assignment operator.
 

Static Private Attributes

static ClassDescription< RandomGeneratorinitRandomGenerator
 Describe a concrete class with persistent data. More...
 

Additional Inherited Members

- Public Attributes inherited from ThePEG::Pointer::ReferenceCounted
const unsigned long uniqueId
 The unique ID. More...
 
- Static Protected Member Functions inherited from ThePEG::Interfaced
static void registerRepository (IBPtr)
 Register an Interfaced object with the Repository.
 
static void registerRepository (IBPtr, string newName)
 Register an Interfaced object with the Repository, giving it a name.
 

Detailed Description

RandomGenerator is an interface to the CLHEP::RandomEngine classes.

To avoid excessive virtual function calls, the RandomGenerator caches random numbers generated by the engine which are then retrieved by the non-virtual inlined rnd() method.

Sub-classes of RandomGenerator should be used to implement a particular random engine.

RandomGenerator only provides a flat distribution between 0 and

  1. Any other distribution can be achieved using the CLHEP random classes using the engine returned from the randomGenerator() method.
See also
The interfaces defined for RandomGenerator.
UseRandom

Definition at line 37 of file RandomGenerator.h.

Member Typedef Documentation

◆ RndVector

typedef vector<double> ThePEG::RandomGenerator::RndVector

A vector of doubles.

Definition at line 42 of file RandomGenerator.h.

◆ size_type

typedef RndVector::size_type ThePEG::RandomGenerator::size_type

The size_type of RndVector.

Definition at line 45 of file RandomGenerator.h.

Member Function Documentation

◆ doinit()

virtual void ThePEG::RandomGenerator::doinit ( )
protectedvirtual

Initializes this random generator.

This should be done first of all before the initialization of any other object associated with an event generator.

Reimplemented from ThePEG::InterfacedBase.

Referenced by rnd().

◆ flush()

void ThePEG::RandomGenerator::flush ( )
inline

Discard all random numbers in the cache.

Typically used after the internal random engine has been reinitialized for some reason.

Definition at line 352 of file RandomGenerator.h.

References gaussSaved, nextNumber, and theNumbers.

◆ operator()()

double ThePEG::RandomGenerator::operator() ( double  N)
inline

Return a (possibly cached) flat integer random number in the interval $[0,N[$.

Function was introduced since otherwise operator()() is used if a double is given resulting in a $]0,1[$ distribution.

Definition at line 119 of file RandomGenerator.h.

References rnd().

◆ persistentInput()

void ThePEG::RandomGenerator::persistentInput ( PersistentIStream is,
int  version 
)

Function used to read in object persistently.

Parameters
isthe persistent input stream read from.
versionthe version number of the object when written.

Referenced by rnd().

◆ persistentOutput()

void ThePEG::RandomGenerator::persistentOutput ( PersistentOStream os) const

Function used to write out object persistently.

Parameters
osthe persistent output stream written to.

Referenced by rnd().

◆ prndbool() [1/2]

bool ThePEG::RandomGenerator::prndbool ( double  p = 0.5)

Return a true with probability p.

Uses rnd(). Also uses push_back(double).

Referenced by rndbool().

◆ prndbool() [2/2]

bool ThePEG::RandomGenerator::prndbool ( double  p1,
double  p2 
)
inline

Return a true with probability p1/(p1+p2).

Uses rnd(). Also uses push_back(double).

Definition at line 150 of file RandomGenerator.h.

References prndbool(), prndsign(), and rndsign().

Referenced by prndbool().

◆ prndsign()

int ThePEG::RandomGenerator::prndsign ( double  p1,
double  p2,
double  p3 
)

Return -1, 0, or 1 with relative probabilities p1, p2, p3.

Uses rnd(). Also uses push_back(double).

Referenced by prndbool().

◆ push_back()

void ThePEG::RandomGenerator::push_back ( double  r)
inline

Give back a partly unused random number.

This is typically used when generating integral random numbers. In eg. rndbool(double p) a random number r is drawn and if it is less than p true is returned, but r is still a good random number in the interval ]0,p[. Hence r/p is still a good random number in the interval ]0,1[ and this is then pushed back into the cache and is used by the next call to rnd(). Note that the resulting random number is of lesser quality, and successive calls to push_back() should be avoided. To ensure a highest quality random number random number in the next call to rnd(), pop_back() should be used.

Definition at line 336 of file RandomGenerator.h.

References nextNumber, and theNumbers.

◆ rnd2()

int ThePEG::RandomGenerator::rnd2 ( double  p0,
double  p1 
)
inline

Return an integer $i$ with probability p $i$/(p0+p1).

Uses rnd().

Definition at line 168 of file RandomGenerator.h.

References rndbool().

Referenced by ThePEG::UseRandom::rnd2().

◆ rnd3()

int ThePEG::RandomGenerator::rnd3 ( double  p0,
double  p1,
double  p2 
)
inline

Return an integer $i$ with probability p $i$/(p0+p1+p2).

Uses rnd().

Definition at line 176 of file RandomGenerator.h.

References rnd4(), rnd5(), and rndsign().

Referenced by ThePEG::UseRandom::rnd3().

◆ rnd4()

int ThePEG::RandomGenerator::rnd4 ( double  p0,
double  p1,
double  p2,
double  p3 
)

Return an integer/ $i$ with probability p $i$(p0+p1+p2+p3).

Uses rnd().

Referenced by rnd3(), and ThePEG::UseRandom::rnd4().

◆ rnd5()

int ThePEG::RandomGenerator::rnd5 ( double  p0,
double  p1,
double  p2,
double  p3,
double  p4 
)

Return an integer/ $i$ with probability p $i$(p0+p1+p2+p3+p4).

Uses rnd().

Referenced by rnd3(), and ThePEG::UseRandom::rnd5().

◆ rndbool() [1/2]

bool ThePEG::RandomGenerator::rndbool ( double  p = 0.5)
inline

Return a true with probability p.

Uses rnd().

Definition at line 130 of file RandomGenerator.h.

References prndbool(), and rnd().

Referenced by ThePEG::UseRandom::prndbool(), rnd2(), and ThePEG::UseRandom::rndbool().

◆ rndbool() [2/2]

bool ThePEG::RandomGenerator::rndbool ( double  p1,
double  p2 
)
inline

Return a true with probability p1/(p1+p2).

Uses rnd().

Definition at line 144 of file RandomGenerator.h.

References rndbool().

Referenced by rndbool().

◆ rndBW()

template<typename Unit >
Unit ThePEG::RandomGenerator::rndBW ( Unit  mean,
Unit  gamma,
Unit  cut 
)
inline

Return a positive number distributed according to a non-relativistic Breit-Wigner with a given width, gamma, and a given mean.

The distribution is cut-off so that the number is between mean - cut and mean + cut

Definition at line 278 of file RandomGenerator.h.

References rnd().

◆ rndGauss()

double ThePEG::RandomGenerator::rndGauss ( )
inline

Return a number distributed according to a Gaussian distribution with zero mean and unit variance.

A second number is cached and returned the next time. This function calls the rndGaussTwoNumbers function which returns two numbers at once.

Definition at line 227 of file RandomGenerator.h.

References gaussSaved, rndGaussTwoNumbers(), and savedGauss.

Referenced by ThePEG::UseRandom::rndGauss(), and rndGauss().

◆ rndGaussTwoNumbers()

void ThePEG::RandomGenerator::rndGaussTwoNumbers ( double &  randomNumberOne,
double &  randomNumberTwo 
)
inline

Return two numbers distributed according to a Gaussian distribution with zero mean and unit variance.

Parameters
[out]Firstrandom number
[out]Secondrandom number

Definition at line 214 of file RandomGenerator.h.

References ThePEG::Constants::pi, and rnd().

Referenced by rndGauss(), and rndGaussTwoNumbers().

◆ rndPoisson()

long ThePEG::RandomGenerator::rndPoisson ( double  mean)

Return a non-negative number generated according to a Poissonian distribution with a given mean.

Warning: the method implemented is very slow for large mean and large return values. For this reason the maximum return value is given by nmax.

Referenced by ThePEG::UseRandom::rndPoisson(), and rndRelBW().

◆ rndRelBW()

template<typename Unit >
Unit ThePEG::RandomGenerator::rndRelBW ( Unit  mean,
Unit  gamma,
Unit  cut 
)
inline

Return a positive number distributed according to a relativistic Breit-Wigner with a given width, gamma, and a given mean.

The distribution is cut-off so that the number is between mean - cut and mean + cut

Definition at line 302 of file RandomGenerator.h.

References rnd(), rndPoisson(), and ThePEG::sqr().

◆ rndsign()

int ThePEG::RandomGenerator::rndsign ( double  p1,
double  p2,
double  p3 
)

Return -1, 0, or 1 with relative probabilities p1, p2, p3.

Uses rnd().

Referenced by prndbool(), ThePEG::UseRandom::prndsign(), rnd3(), and ThePEG::UseRandom::rndsign().

◆ setSeed()

virtual void ThePEG::RandomGenerator::setSeed ( long  seed)
pure virtual

Reset the underlying random engine with the given seed.

If the seed is set to -1 a standard seed will be used.

Implemented in ThePEG::StandardRandom.

Member Data Documentation

◆ initRandomGenerator

ClassDescription<RandomGenerator> ThePEG::RandomGenerator::initRandomGenerator
staticprivate

Describe a concrete class with persistent data.

Note that the class should in principle be abstract.

Definition at line 458 of file RandomGenerator.h.


The documentation for this class was generated from the following file: