Random number generationΒΆ

Random number generation is widely used within GammaLib for drawing event distributions from functions.

C++

1
2
3
4
5
GRan ran;                              // Construct random number generate instance
double uniform     = ran.uniform();    // Uniform random number
double exponential = ran.exp(3.7);     // Exponential random number
double poisson     = ran.poisson(2.4); // Poisson random number
double chi2        = ran.chisq2();     // Chi2 random number

Python

1
2
3
4
5
ran         = gammalib.GRan()          # Construct random number generate instance
uniform     = ran.uniform()            # Uniform random number
exponential = ran.exp(3.7)             # Exponential random number
poisson     = ran.poisson(2.4)         # Poisson random number
chi2        = ran.chisq2()             # Chi2 random number

In line 1 a random number generator is allocated. If control over the seed value of the random number generator is needed (for example to draw different samples), you may specify the seed value upon construction:

C++

1
2
unsigned long long int seed = 123456;
GRan ran(seed);

Python

1
2
seed = 123456
ran  = gammalib.GRan(seed)

The GRan::uniform() method returns a random number between 0 and 1. The GRan::exp() method returns a random number of the exponential law

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

where \(\lambda\) is the parameter of the distribution. In line 2 above, \(\lambda=3.7\). 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.

The GRan::poisson() method draws a random number from the Poisson distribution. You mya use this method to simulate the number of events in case that a given mean number \(\lambda\) of events is known. In line 3 above, \(\lambda=2.4\).

The GRan::chisq2() method draws random numbers from the propability distribution

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

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.