Generates random numbers following a standard gamma distribution.

You're viewing an older version of this page (#235). View the current version.

View versions (1)

Interface

Overview

The general formula for the probability density function of the gamma distribution is

f(x) = \frac{ \left( \frac{x - \mu}{\beta} \right) ^ {\alpha - 1}
    \mathrm{exp} \left( - \frac{x - \mu}{\beta} \right) } { \beta \Gamma (\alpha) }
    \qquad x \geq \mu \qquad \alpha, \beta > 0
(1)

where \alpha is the <em>shape parameter</em>, \mu is the <em>location parameter</em>, \beta is the <em>scale parameter</em>,

\Gamma(a) = \int_0^{\infty} t ^ {a - 1} \mathrm{e} ^ {- t} \mathrm{d} t
(2)

is the gamma function.

The case where \mu = 0 and \beta = 1 is called the standard gamma distribution. Therefore the density of this particular distribution, which we also use with this random number generator is

f(x) = \frac{x ^ {\alpha - 1} \mathrm{e} ^ {- x}}{\Gamma(\alpha)}
    \qquad x \geq 0 \qquad \alpha > 0
(3)

Using this class, the diagram below is generated from two distinct sequences of 1000 random numbers. Each pair of numbers are plotted against each other, to illustrate the standard gamma behaviour of this non-uniform random number generator.

1/gamma-378.png

Speed:

The average running time for generating 100,000,000 random numbers using this class on a 750MHz microprocessor is 38 seconds.

References

  • NIST/SEMATECH e-Handbook of Statistical Methods, http://www.itl.nist.gov/div898/handbook/
  • The Newran03 random number generator library of Robert Davies, http://www.robertnz.net/nr03doc.htm

Example 1

The following example displays 40 random floating point numbers from a standard gamma distribution. It uses two different generators to achieve this. The first generator uses a particular value to initialize the seed, while the second one is using the system timer. Notice that it was necessary to divide the timer with the MERSENNEDIV value in order to keep the seed in the (0, 1) interval. Since the seed of the first generator is never changed, the first 20 numbers will always remain the same. However since the second generator is initialized via the system timer, the next 20 numbers will obviously vary with each execution of the program,

#include <iostream>
#include <time.h>
#include <codecogs/stats/dists/continuous/gamma/randomsample.h>
using namespace std;

int main() 
{
    Stats::Dists::Continuous::Gamma::RandomSample A(3.71, 0.175);
    Stats::Dists::Continuous::Gamma::RandomSample B(4.85, time(0) / MERSENNEDIV);

    for (int i = 0; i < 20; ++i)
        cout << A.genReal() << endl;
    cout << endl;

    for (int i = 0; i < 20; ++i)
        cout << B.genReal() << endl;
    return 0;
}

Below you will find 20 numbers corresponding to the output of the first generator :

6.87529
7.7321
4.06328
5.90752
9.7893
10.2651
3.52183
3.73508
2.43065
0.505826
1.67514
2.52252
4.4189
2.71961
2.33248
4.09904
4.40664
4.07805
1.78729
3.71478
GPL Licence — free for non commercial use. See Licence details.

Members of RandomSample

CLASS METHOD

RandomSample

Class constructor.

CLASS METHOD

RandomSample

Class destructor.

CLASS METHOD

init

reinitialise generator with a new set value for alpha

CLASS METHOD

genReal

Generates a random deviate from the standard gamma distribution.

CLASS METHOD

RandomSample

Constructor that sets up the class variables and initializes the associated random number generator with the given seed.

CLASS METHOD

init

CLASS METHOD

Build1

CLASS METHOD

Build2

CLASS METHOD

Density1

CLASS METHOD

Density2

CLASS METHOD

sample

This function is a simple wrapper around the randomsample class provided in this module. It uses a static to keep a single instance of this class, so that each call to this function returns a new random number. As a result this function is not necessarily thread safe, in the sense that with identical initial seed, the sequence of random numbers may differ on a multitasking or multi threaded system.

The seed is only set on the first call to this function. Thereafter this parameter is ignored. If you do no want to set the seed, then we suggest you use the system clock the first time you call this function, i.e.

#include <time.h>
...
sample(alpha, time(0) / MERSENNEDIV);

If you require more advance behaviour, we strongly recommend that directly use the underlying class randomsample that is provided with this module.

Parameters

seed
sets the initial seed for the random generator. Only used in the first call to this function