GammaLib  1.7.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GRan Class Reference

Random number generator class. More...

#include <GRan.hpp>

Inheritance diagram for GRan:
GBase

Public Member Functions

 GRan (void)
 Void constructor. More...
 
 GRan (unsigned long long int seed)
 Seed constructor. More...
 
 GRan (const GRan &ran)
 Copy constructor. More...
 
virtual ~GRan (void)
 Destructor. More...
 
GRanoperator= (const GRan &ran)
 Assignment operator. More...
 
void clear (void)
 Clear random number generator. More...
 
GRanclone (void) const
 Clone random number generator. More...
 
std::string classname (void) const
 Return class name. More...
 
void seed (unsigned long long int seed)
 Set seed of random number generator. More...
 
unsigned long long int seed (void) const
 Return seed value. More...
 
unsigned long int int32 (void)
 Return 32-bit random unsigned integer. More...
 
unsigned long long int int64 (void)
 Return 64-bit random unsigned integer. More...
 
double uniform (void)
 Returns random double precision floating value in range 0 to 1. More...
 
double normal (void)
 Returns normal deviates. More...
 
double exp (const double &lambda)
 Returns exponential deviates. More...
 
double poisson (const double &lambda)
 Returns Poisson deviates. More...
 
double chisq2 (void)
 Returns Chi2 deviates for 2 degrees of freedom. More...
 
int cdf (const std::vector< double > &cdf)
 Random sampling from a cumulative density function. More...
 
int cdf (const GVector &cdf)
 Random sampling from a cumulative density function. More...
 
std::string print (const GChatter &chatter=NORMAL) const
 Print random number generator information. More...
 
- Public Member Functions inherited from GBase
virtual ~GBase (void)
 Destructor. More...
 

Protected Member Functions

void init_members (unsigned long long int seed=41L)
 Initialise class members. More...
 
void copy_members (const GRan &ran)
 Copy class members. More...
 
void free_members (void)
 Delete class members. More...
 

Protected Attributes

unsigned long long int m_seed
 Random number generator seed. More...
 
unsigned long long int m_value1
 Value 1. More...
 
unsigned long long int m_value2
 Value 2. More...
 
unsigned long long int m_value3
 Value 3. More...
 
double m_old_lambda
 Old lambda value. More...
 
double m_sqrt_lambda
 sqrt(2*lambda) More...
 
double m_log_lambda
 log(lambda) More...
 
double m_exp_lambda
 exp(-lambda) More...
 

Detailed Description

Random number generator class.

This class implements a random number generator.

Definition at line 44 of file GRan.hpp.

Constructor & Destructor Documentation

GRan::GRan ( void  )

Void constructor.

Definition at line 57 of file GRan.cpp.

References init_members().

Referenced by clone().

GRan::GRan ( unsigned long long int  seed)

Seed constructor.

Parameters
[in]seedRandom number generator seed.

Definition at line 72 of file GRan.cpp.

References init_members().

GRan::GRan ( const GRan ran)

Copy constructor.

Parameters
[in]ranRandom number generator.

Definition at line 87 of file GRan.cpp.

References copy_members(), and init_members().

GRan::~GRan ( void  )
virtual

Destructor.

Definition at line 103 of file GRan.cpp.

References free_members().

Member Function Documentation

int GRan::cdf ( const std::vector< double > &  cdf)

Random sampling from a cumulative density function.

Parameters
[in]cdfArray containing cumulative density function

Definition at line 390 of file GRan.cpp.

References uniform().

int GRan::cdf ( const GVector cdf)

Random sampling from a cumulative density function.

Parameters
[in]cdfVector containing cumulative density function
Todo:
Somehow merge with the above method to reduce code repetition

Definition at line 421 of file GRan.cpp.

References GVector::size(), and uniform().

double GRan::chisq2 ( void  )

Returns Chi2 deviates for 2 degrees of freedom.

Returns exponential deviates from the probability distribution

\[p(x) = \frac{1}{2\pi} x \exp( -\frac{1}{2} x^2 )\]

This method can be used to simulate the random radial offset of a measured source position from the true source position, assuming an azimuthally symmetric 2D Gaussian probability distribution.

Definition at line 370 of file GRan.cpp.

References log(), sqrt(), and uniform().

Referenced by GModelSpatialRadialGauss::mc(), GCTAPsfPerfTable::mc(), GCTAPsfVector::mc(), and GCTAPsf2D::mc().

std::string GRan::classname ( void  ) const
inlinevirtual

Return class name.

Returns
String containing the class name ("GRan").

Implements GBase.

Definition at line 99 of file GRan.hpp.

void GRan::clear ( void  )
virtual

Clear random number generator.

This method properly resets the object to an initial state using the default seed.

Implements GBase.

Definition at line 158 of file GRan.cpp.

References free_members(), and init_members().

GRan * GRan::clone ( void  ) const
virtual

Clone random number generator.

Returns
Pointer to deep copy of random number generator.

Implements GBase.

Definition at line 176 of file GRan.cpp.

References GRan().

void GRan::copy_members ( const GRan ran)
protected

Copy class members.

Parameters
[in]ranRandom number generator.

Definition at line 513 of file GRan.cpp.

References m_exp_lambda, m_log_lambda, m_old_lambda, m_seed, m_sqrt_lambda, m_value1, m_value2, and m_value3.

Referenced by GRan(), and operator=().

double GRan::exp ( const double &  lambda)

Returns exponential deviates.

Parameters
[in]lambdaMean rate.

Returns exponential deviates from the probability distribution

\[p(x) = \lambda \exp( -\lambda x )\]

where \(\lambda\) is the parameter of the distribution. This method may be used to simulate the occurence time of an event, where \(\lambda\) is the mean event rate. Convsersely, \(1/\lambda\) is the mean waiting time between events.

Todo:
Check that \(\lambda>0\).

Definition at line 291 of file GRan.cpp.

References log(), and uniform().

Referenced by GModelTemporalConst::mc(), and GModelTemporalPhaseCurve::mc().

void GRan::free_members ( void  )
protected

Delete class members.

Definition at line 533 of file GRan.cpp.

Referenced by clear(), operator=(), seed(), and ~GRan().

void GRan::init_members ( unsigned long long int  seed = 41L)
protected

Initialise class members.

Definition at line 484 of file GRan.cpp.

References int64(), m_exp_lambda, m_log_lambda, m_old_lambda, m_seed, m_sqrt_lambda, m_value1, m_value2, m_value3, and seed().

Referenced by clear(), GRan(), operator=(), and seed().

unsigned long int GRan::int32 ( void  )

Return 32-bit random unsigned integer.

Definition at line 206 of file GRan.cpp.

References int64().

unsigned long long int GRan::int64 ( void  )

Return 64-bit random unsigned integer.

Definition at line 216 of file GRan.cpp.

References m_value1, m_value2, and m_value3.

Referenced by init_members(), int32(), and uniform().

double GRan::normal ( void  )

Returns normal deviates.

Method Used: Box-Muller transform, outlined here: http://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform

Code from: http://www.design.caltech.edu/erik/Misc/Gaussian.html

Definition at line 257 of file GRan.cpp.

References log(), sqrt(), and uniform().

Referenced by GModelSpatialEllipticalGauss::mc(), GModelSpectralGauss::mc(), and GCTAEdispPerfTable::mc().

GRan & GRan::operator= ( const GRan ran)

Assignment operator.

Parameters
[in]ranRandom number generator.
Returns
Random number generator.

Definition at line 125 of file GRan.cpp.

References copy_members(), free_members(), and init_members().

double GRan::poisson ( const double &  lambda)

Returns Poisson deviates.

Parameters
[in]lambdaExpectation value.

Returns Poisson deviates for an expectation value.

This method may be used to simulate the number of events in case that a given mean number of events is expected.

Definition at line 316 of file GRan.cpp.

References exp(), gammalib::gammln(), log(), m_exp_lambda, m_log_lambda, m_old_lambda, m_sqrt_lambda, gammalib::pi, sqrt(), tan(), and uniform().

Referenced by GModelTemporalLightCurve::mc().

std::string GRan::print ( const GChatter chatter = NORMAL) const
virtual

Print random number generator information.

Parameters
[in]chatterChattiness (defaults to NORMAL).
Returns
String containing random number generator information.

Implements GBase.

Definition at line 451 of file GRan.cpp.

References m_seed, m_value1, m_value2, m_value3, gammalib::parformat(), SILENT, and gammalib::str().

void GRan::seed ( unsigned long long int  seed)

Set seed of random number generator.

Parameters
[in]seedRandom number generator seed.

This method properly resets the object to an initial state using the specified seed.

Definition at line 190 of file GRan.cpp.

References free_members(), and init_members().

unsigned long long int GRan::seed ( void  ) const
inline

Return seed value.

Returns
Seed value.

Definition at line 111 of file GRan.hpp.

References m_seed.

Referenced by init_members().

Member Data Documentation

double GRan::m_exp_lambda
protected

exp(-lambda)

Definition at line 89 of file GRan.hpp.

Referenced by copy_members(), init_members(), and poisson().

double GRan::m_log_lambda
protected

log(lambda)

Definition at line 88 of file GRan.hpp.

Referenced by copy_members(), init_members(), and poisson().

double GRan::m_old_lambda
protected

Old lambda value.

Definition at line 86 of file GRan.hpp.

Referenced by copy_members(), init_members(), and poisson().

unsigned long long int GRan::m_seed
protected

Random number generator seed.

Definition at line 80 of file GRan.hpp.

Referenced by copy_members(), init_members(), print(), and seed().

double GRan::m_sqrt_lambda
protected

sqrt(2*lambda)

Definition at line 87 of file GRan.hpp.

Referenced by copy_members(), init_members(), and poisson().

unsigned long long int GRan::m_value1
protected

Value 1.

Definition at line 81 of file GRan.hpp.

Referenced by copy_members(), init_members(), int64(), and print().

unsigned long long int GRan::m_value2
protected

Value 2.

Definition at line 82 of file GRan.hpp.

Referenced by copy_members(), init_members(), int64(), and print().

unsigned long long int GRan::m_value3
protected

Value 3.

Definition at line 83 of file GRan.hpp.

Referenced by copy_members(), init_members(), int64(), and print().


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