C++11 negative binomial distribution class random number generator
The C++11 negative binomial distribution (or negative_binomial_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 negative_binomial_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 but how this type is defined is compiler dependent.And note param_type must have it’s type same as the distribution type.
Constructors and reset function
explicit negative_binomial_distribution(IntType k = 1, double p = 0.5); explicit negative_binomial_distribution(const param_type& parm); void reset();
The first constructor accept two parameters ‘k’ and ‘p’,these two parameters are used by the probability function to calculate the probability of the random number.By default their values are 1 and 0.5 and they must hold the relation 0 < p ≤ 1 and 0 < k.
The second constructor accept a reference to param_type object.In this case the value of ‘k’ and ‘p’ is taken from the value of ‘k’ and ‘p’ of the param_type constructor-param_type constructor also accept two parameters ‘k’ and ‘p’ similar to the distribution constructor.
negative_binomial_distribution< long > nbd( 45 , 0.23); // k is 45 and p is 0.23 negative_binomial_distribution< long long >::param_type pt( 888 , 0.43 ); negative_binomial_distribution< long long > nbd1( pt) ; // k is 888 and p is 0.43 taken from the pt object negative_binomial_distribution< long > nbd2( pt) ; //error! /*type of pt is long long but type of nbd2 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 operator() function provide you an access to the random integer sequence.The first operator() function accept URNG(Uniform Random Number Generator) or engine.
negative_binomial_distribution< long > nbd( 45 , 0.23); linear_congruential_engine<unsigned int , 193703 , 0 , 83474882 > lce , lce1 ; cout<< nbd(lce) << ” ” << nbd(lce) << endl ; negative_binomial_distribution< long long >::param_type pt( 888 , 0.43 ); negative_binomial_distribution< long long > nbd1( pt) ; cout<< nbd1(lce1) << ” ” << nbd1(lce1) << endl ;
Output in cede::blocks,
1220 1190
the second operator() function
The second overloaded operator() function accept URNG and param_type object.
negative_binomial_distribution< long long > nbd ; negative_binomial_distribution< long long >::param_type pt( 1200 , 0.93 ); cout<< nbd1(lce1) << ” ” << nbd1(lce1) << endl ;
Output in Code::blocks,
Property functions
result_type k( ) const ; result_type p() const; param_type param() const; void param(const param_type& parm); result_type min() const; result_type max() const;
k() function
This functions returns the value of ‘k’ parameter.
negative_binomial_distribution< long > nbd( 45 , 0.23); cout<< nbd.k( ) ;
Output ,
p() function
This functions returns the value of ‘p’ parameter.
negative_binomial_distribution< long >::patam_type pt(888 , 0.43); cout<< pt.p() ;
Output ,
param()
This function returns the param_type object.
negative_binomial_distribution< long >::param_type pt( 45 , 0.23); cout<< pt.k( ) << endl ; //checking the value of ‘k’ negative_binomial_distribution< long > nbd( 500 , 0.6666 ); pt=nbd.param( ); cout<< pt.k( ) ;
Output,
500
Assigning pt with the returned value of nbd will change it’s ‘k’ and ‘p’ value to 500 and 0.6666.
param(param_type)
This function sets the parameter set of the distribution,in other words we can say it changes the ‘k’ and ‘p’ value of the distribution to the value of the ‘k’ and ‘p’ of the pram_type object passed as argument.
negative_binomial_distribution< > nbd ; cout<< nbd.k() << endl ; negative_binomial_distribution< >::param_type pt( 900 , 0.625) ; nbd.param( pt ); cout<< nbd.param( ).k( ) ;
Output ,
900
min() function
The min() returns the smallest value the distribution can generate,which is the value 0
negative_binomial_distribution< > nbd ; cout<<nbd.min( ) ;
Output,
max() function
The max() returns the largest value the distribution can generate.It returns the value of numeric_limits<result_type>::max().
negative_binomial_distribution< > nbd ; cout<<nbd.max( ) ;
Output,
The largest value ‘int’ type can represent is 2147483647 which is also the value of numeric_limits<int>::max()
*Side note
negative_binomial_distribution produces random integers i ≥ 0 distributed according to the discrete probability function,