Random number generationΒΆ

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

C++

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

Python

1ran         = gammalib.GRan()          # Construct random number generate instance
2uniform     = ran.uniform()            # Uniform random number
3exponential = ran.exp(3.7)             # Exponential random number
4poisson     = ran.poisson(2.4)         # Poisson random number
5chi2        = 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++

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

Python

1seed = 123456
2ran  = 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.