C++11 binomial distribution random number generator
The C++11 binomial distribution (or binomial_distribution) generates integer random sequence equal to or greater than 0.
Link :C++11 random number generator
The class declaration is shown below.
template<class IntType = int> class binomial_distribution;
The class types and the member functions are given below.
Types
typedef IntType result_type; typedef unspecified param_type;
The param_type is a structure but how this type is defined will be different in different compiler.The probable use of this structure is to store the information of the distribution such as the largest value the distribution can generate.
Constructors and reset function
explicit binomial_distribution(IntType t = 1, double p = 0.5); explicit binomial_distribution(const param_type& parm); void reset()
In the first constructor it accept two parameters ‘t’ and ‘p’.The ‘t’ parameter determines the largest value the distribution can generate and the ‘p’ parameter is used for calculating the probability of an integer in the distribution.They must hold the relation : 0 ≤ p ≤ 1 and 0 ≤ t .
The second constructor accept a param_type object and the value of ‘t’ and ‘p’ is assign the same value as the value of ‘t’ and ‘p’ of the param_type object-note param_type constructor also accept ‘t’ and ‘p’ parameters and their default values is same as the default values of the distribution.The relation 0 ≤ p ≤ 1 and 0 ≤ t still holds for the param_type parameter.
binomial_distribution< unsigned > bd(89); //limits the range of sequence generation to 89 binomial_distribution< >::param_type pt( 56 ); binomial_distribution< > bd1( pt ); //bd1 integers generated range is limited to 56
The parm_type has a function name p() that renders the value of ‘p’ ,an example is shown later.
reset() function
This function resets the distribution.It does nothing in binomial_distribution.
Generating functions
The C++11 binomial distribution has two generating functions.By generating function we mean the functions which generate the random sequence.
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 operator() function provide you an access to the random sequence.The first operator accept URNG(Uniform Random Number Generator) or engine.
binomial_distribution< unsigned > bd(89); default_random_engine dre ; cout<< bd(dre) << ” ” << bd(dre) << endl ; cout<<“\n\n” ; binomial_distribution< >::param_type pt( 56 ); binomial_distribution< > bd1( pt ); //passing param_type object cout<< bd1(dre) << ” ” << bd1(dre) << endl ; cout<< pt.p( ) ; //calling the p() function
Output in Code::Blocks,
22 27
0.5
The p() returns 0.5 because it is the default value of ‘p’,you can also replace it with any values.
Property functions
IntType t() const; double p() const; param_type param() const; void param(const param_type& parm); result_type min( ) const; result_type max( ) const;
t() function
This function returns the value of ‘t’ parameter.
binomial_distribution< unsigned > bd(89); cout<< bd.t();
Output,
p() function
This function returns the value of ‘p’.
binomial_distribution< unsigned > bd(89 , 0.90 ); cout<< bd.p() ;
Output,
param()
This function returns the parm_type object.
binomial_distribution< unsigned > bd(89 , 0.90 ); binomial_distribution< unsigned >::param_type pt; pt=bd.param(); cout<< pt.t() << ” ” << bd.param().t() << endl ;
Output,
Calling bd.param().t() also gives the value of ‘t’.
param(param_type)
This function sets the parameter set of the distribution,in other words we can say it changes the ‘t’ and ‘p’ value of the param_type object.
binomial_distribution< unsigned > bd(89 , 0.90 ); cout<< bd.t() << ” ” << bd.p() << endl ; binomial_distribution< unsigned >::param_type pt( 120 , 0.34); bd.param( pt ); //changes the value of ‘t’ and ‘p’ cout<< bd.t() << ” ” << bd.p() << endl ;
Output,
120 0.34
After calling bd.param() the value of ‘t‘ and ‘p‘ of bd is changed to 120 and 0.34.
min() function
min() gives the minimum value the distribution can generate.
binomial_distribution< int> bd; cout<< bd.min() ;
Output
max() function
max() gives the maximum value the distribution can generate.This function is technically similar to t() function.
binomial_distribution< int> bd( 999 , 0.3); cout<< bd.max() ;
Output
Side note
binomial_distribution produces integer values i≥0 distributed according to the discrete probability function,