C++11 poisson distribution random number generator

The C++11 poisson distribution( or poisson_distribution) produces random integers i≥0,this distribution can generate only integers sequence.


Link :C++11 random number generator

The declaration of the class is shown below.

template<class IntType = int>
class poisson_distribution;

The default type of the class template is ‘int’ type.All the types and member functions of the class is shown below.


Types

typedef IntType result_type; 

typedef unspecified param_type; 

The param_type is a structure and it’s definition is compiler dependent.And note param_type must have it’s type same as the distribution type.


Constructors and reset function

explicit poisson_distribution(double mean = 1.0); 

explicit poisson_distribution(const param_type& parm); 

void reset();

The first constructor accept a parameter known as ‘mean’ ,this parameter is used in calculating the probability of this random integers in this distribution.The ‘mean’ must be always greater than 0.

The second constructor accept a reference to param_type object.Here the ‘mean’ value of the distribution is initialized with the ‘mean’ parameter of the param_type object-param_type constructor accept ‘mean’ parameter.

poisson_distribution< long > pd( 15 ); //mean value is 15
poisson_distribution<signed long long >::param_type pt( 88 );

poisson_distribution<signed long long > pd1( pt) ; //mean value is 88 taken from the ‘mean’ value of the pt object

poisson_distribution< long > pd2( pt) ; //error! , type of pt is signed long long but type of pd2 is long type

reset( ) function

This function resets the distribution.It does nothing in negative_binomial_distribution.You can neglect it.



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.

poisson_distribution< long > pd( 15 );
default_random_engine dre;

cout<< pd(dre) << ” ” << pd(dre) ;

poisson_distribution<signed long long >::param_type pt( 88 );

poisson_distribution<signed long long > pd1( pt) ;

cout<< pd1(dre) << ” ” << pd1(dre) ;

Output in CB,

16   11
92   82

the second operator() function

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

poisson_distribution< long > pd ;

poisson_distribution< long >::param_type pt( 100 );

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

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

Output in CB,

89   91


Property functions

double mean() const

param_type param() const; 

void param(const param_type& parm);

result_type min() const; 

result_type max() const; 
mean() function

This function returns the value of ‘mean’ parameter.

poisson_distribution< long > pd( 15 );

cout<< pd.mean() ;

Output,

15

param()

This function returns the param_type object.

poisson_distribution<signed long long >::param_type pt( 88 );

cout<< pt.mean() << endl ;

poisson_distribution<signed long long > pd(200) ;

pt=pd.param( );

cout<< pt.mean() << ” ” << pd.param().mean() endl ;

Output,

88
200   200

param(param_type)

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

poisson_distribution< long >pd ;
cout<< pd.mean() << endl ;

poisson_distribution< long >::param_type pt( 500 ) ;

pd.param( pt );

cout<< pd.mean() ;

Output,

1
500

min() function

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

poisson_distribution< > pd ;
cout<< pd.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().

poisson_distribution<signed long long > pd ;
cout<< pd.max( ) ;

Output,

9223372036854775807


Side note

poisson_distribution produces integer values i≥0 distributed according to the discrete probability function,

poisson_distribution probability function

μ is the ‘mean’.