C++11 gamma distribution random number generator

The C++11 gamma distribution (or gamma distribution) produces random numbers x > 0 using the respective discrete probability function of the distribution-the function is shown at the end of the post.

Link :C++11 random number generator
 
C++11 gamma distribution

The distribution class declaration is shown below.

template<tclass RealType = double>
class gamma_distribution;

The distribution produces only floating point values and the default type of the distribution is double type.You can change the type by passing it as template argument type during the distribution object declaration.

All the types and member functions of the distribution is shown below.


Types

typedef RealType result_type;

typedef unspecified param_type; 

The RealType is a type definition of the distribution type.And the param_type is a structure,however the definition will vary from compiler to compiler.



Constructors and reset function

explicit gamma_distribution(RealType alpha = 1.0 , RealType beta = 1.0 ); 

explicit gamma_distribution(const param_type& parm); 

void reset( );

The first constructor accept parameters known as ‘alpha’ and ‘beta’,they are used in calculating the probability of the random values in the distribution.The relation 0 < alpha and 0 < beta.

The second constructor accept a reference to param_type object.Here the values of ‘alpha’ and ‘beta’ of the distribution object is initialized with the ‘alpha’ and ‘beta’ values of the param_type object-param_type constructor also accept the parameters name ‘alpha’ and ‘beta’.

gamma_distribution< > gd(5.6 , 4.5);

gamma_distribution<float >::param_type pt( 8.8 , 9.9 );

gamma_distribution<float > gd1( pt) ; //alpha is 8.8 and beta is 9.9 which are assigned from the ‘alpha’ and ‘beta’ of the pt object

gamma_distribution< > gd2( pt ) ; //error! , type of pt is float but type of gd2 is double type

Generating functions

template<class URNG>
result_type operator( )(URNG& g); 

template<class URNG>

result_type operator( )(URNG& g, const param_type& parm); 
the first operator() function

The generated random sequence is obtained using the operator() function.The first overloaded operator() accept URNG(Uniform Random Number Generator) or engine.

gamma_distribution< > gd(5.6 , 4.5);

default_random_engine dre ;

cout<< gd( dre ) << ” ” << gd( dre ) ; 

Output in Code::blocks,

14.1548   22.4626

the second operator( ) function

The second overloaded operator( ) function accept URNG and param_type object.

gamma_distribution< > gd ;

gamma_distribution<float >::param_type pt( 8.8 , 9.9 );

linear_congruential_engine<unsigned int , 193703 , 0 , 83474882 > lce ;

cout<< gd( lce , pt ) << ” ” << gd( lce , pt ) ;

Output in Code::blocks,

60.2303   94.3773



Property functions

RealType alpha() const;

RealType beta() const; 

param_type param() const;

void param(const param_type& parm); 

result_type min() const;

result_type max() const; 
alpha() function

This function returns the ‘alpha’ value of the distribution.

gamma_distribution< > gd(5.6 , 4.5);

cout<< gd.alpha( );

Output,

5.6

beta() function

This function returns the ‘beta’ value of the distribution.

gamma_distribution< > gd(5.6 , 4.5);

cout<< gd.beta( );

Output,

4.5

param()

This function returns the param_type object.

gamma_distribution<::param_type pt;

cout<< gd.alpha() << endl ;

gamma_distribution< > gd(90.9 , 1234) ;

pt=gd.param( );

cout<< gd.alpha() ;

Output,

1
90.9

param(param_type)

Using this function we can change the ‘alpha’ and ‘beta’ value of the distribution to the ‘alpha’ and ‘beta’ value of the param_type object by passing the param_type object.

gamma_distribution< long double > gd(2.3 , 7.8);

cout<< gd.alpha() << endl ;

gamma_distribution< long double >::param_type pt(2.56 , 9.5675);

gd.param( pt );

cout<< gd.alpha() << ” ” << gd.beta( ) ;

Output,

2.3
2.56   9.5675

min() function

The min() returns the smallest value the distribution can generate,which is the value 0.

gamma_distribution< > gd ;

cout<< gd.min( ) ;

Output,

0

max() function

The max() returns the largest value the distribution can generate.It returns the value of numeric_limits<result_type>::max().

gamma_distribution< > gd ;

cout<< gd.max( ) ;

Output,

1.79769e+308

The numeric_limits<double>::max() also gives the value 1.79769e+308.


Side note

gamma_distribution roduces random numbers x < 0 distributed according to the probability density function,


gamma_distribution probability function