C++11 random geometric_distribution


The C++11 random geometric_distribution produces random integers sequence i ≥ 0.This distribution can only generate integer values.The declaration of the class is shown below.

template
class geometric_distribution;

It is apparent from the declaration that the default type of the class template is ‘int’ type.The types and the member functions of the class is shown below.

Types

typedef IntType result_type;
typedef unspecified param_type;

The param_type is a structure.Note param_type must have it’s type same as the distribution type.

Constructors and reset function

explicit geometric_distribution(double p = 0.5);
explicit geometric_distribution(const param_type& parm);
void reset();

The constructor accept ‘p’ parameter which is use to calculate the probability distribution of the of the random integer(look at the Side note at the end of this post to view the probability function).The default value is 0.5 and it must hold the relation : 0< p < 1 .

The second constructor accept a reference to param_type object.The ‘p’ value in this case is determine from the ‘p’ parameter of the param_type object(param_type constructor also accept ‘p’ parameter).

Code example

geometric_distribution< int > gd(0.67);

geometric_distribution< unsigned >::param_type pt(.902);
geometric_distribution< unsigned > gd1( pt ) ; //’p’ value is 0.902 taken from the ‘p’ value of the pt object

reset() function

This function resets the distribution.It does nothing in geometric_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.

Code example

geometric_distribution< unsigned > gd( 0.67 );

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

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

Output in Code::blocks,

1   0   0

the second operator() function

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

Code example

geometric_distribution< int >::param_type pt( 0.902 );

geometric_distribution< int >gd1 ;

default_random_engine dre ;

cout<< gd1(dre , pt) << ” ” << gd1(dre , pt) << ” ” << gd1(dre, pt) ;

Output in CB,

0 0 0


Property functions

rdouble p() const;
param_type param() const;
void param(const param_type& parm);
result_type min() const;
result_type max() const;

p() function

This function returns the ‘p’ value .

Code example

geometric_distribution< unsigned > gd( 0.354 ) ,
gd1;

cout<< gd.p() << endl
<< gd1.p() ;

Output in CB,

0.354
0.5


param()

This function returns the parm_type object.

Code example

geometric_distribution< >::param_type pt(0.55) ;

cout<< pt.p() << endl ; //access the value of ‘p’

geometric_distribution< >gd( 0.345 );

pt=gd.param(); //assigning the gd pram_type object to pt

cout<< pt.p() ;

Output ,

0.55
0.345

In assigning the returned value of param() function to pt, the ‘p’ value of pt is also changed to the ‘p’ value of ‘gd’.

param(param_type)

This function sets the parameter set of the distribution,in other words we can say it changes the ‘p’ value of the distribution to the value of the ‘p’ of pram_type object passed as argument.

Code example

geometric_distribution< long > gd( 0.11 );

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

geometric_distribution< long >::param_type pt( 0.22);

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

Output ,

0.11
0.22

min() function

The min() returns the smallest value the distribution can generate.

Code example

geometric_distribution< unsigned int> gd;

cout<< gd.min() ;

Output ,

0

max() function

The max() returns the largest value the distribution can generate.The return value is most probably the largest value the type can represent.For instance an unsigned int can represent a largest value of 4294967295, so this value will be the returned value.

Code example

geometric_distribution< unsigned int> gd;

cout<< gd.max() ;

Output ,

4294967295


*Side Note

geometric_distribution roduces integer values i≥0 distributed according to the discrete probability function

  P(i|p) = p·(1−p)i


 


Stop wasting time,earn money($$$) from your website-Join Now!