C++11 bernoulli distribution random number generator

The C++11 random bernoulli distribution (or bernoulli_distribution) produces only random bool values.The output will alter between 1 and 0.

Link : C++11 random number generator

C++11 random bernoulli_distribution

The class declaration is shown below.

class bernoulli_distribution;

The class types and the member functions are given below.


typedef bool result_type; 

typedef unspecified param_type; 

The param_type is not specified and it is compiler dependent.This type however usually is a structure with some of the member functions similar to the member functions of the bernoulli_ditribution class.

Constructors and reset function

C++11 bernoulli distribution has two constructors.

explicit bernoulli_distribution(double p = 0.5); 

explicit bernoulli_distribution(const param_type& parm);

void reset( );

The constructors either accept a double literal or a reference to param_type object.If no argument is passed during the constructor call the default value of ‘p’ is set to 0.5.

Note changing the default value of ‘p’ or passing the param_type object will have an utter influence on the sequence of bool random values generated by the distribution.

bernoulli_distribution bd , //calls the first constructor and set ‘p’ to 0.5

bd1(7.8); //calls the first constructor and set ‘p’ to 7.8

bernoulli_distribution::param_type pt ;

bernoulli_distribution bd2( pt ); //calls the second constructor

Note not only passing param_type object influence the sequence generated but also passing arguments to the param_type constructor affect the sequence generated.To elaborate more on this,the param_type constructor also has a parameter name ‘p’ whose value is set to 0.5.

The sequence generated by the distribution with param_type whose ‘p’ value is set to 0.5 is different from the sequence generated by the distribution having different ‘p’ value.A code example on this is provided under the Generating function section below.

reset() function

The ‘reset()’ function reset the state of the distribution but note in bernoulli_distribution this function does nothing.You can neglect it.

Generating functions

The C++11 bernoulli_distribution generating function generate the random sequence.

URNG stands for Uniform Random Number Generator.

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 will output the random bool sequence.The first operator accept only URNG(Uniform Random Number Generator) as it’s argument,without passing URNG-also known as engine-you cannot call the operator() function.

bernoulli_distribution bd ;

default_random_engine dre ;

cout<< bd( dre ) << ” ” << bd(dre) ;

cout<<“\n\n” ;

bernoulli_distribution::param_type pt , pt1(5.6);

bernoulli_distribution bd1(pt) , //pt has ‘p’ value as 0.5
bd2( pt ); //pt1 has ‘p’ value as 5.6

cout<< bd1( dre ) << ” ” << bd1( dre ) ;
cout<< bd2( dre ) << ” ” << bd2( dre ) ;

Output in Code::Blocks,

1   1
0   0
1   1

You can see that the sequence generated by bd1 and bd2 is different since the param_type object passed to bd1 and bd2 has it’s parameter ‘p’ value assigned differently.

The second operator() function

The second operator() is called only when you want to use an engine and a param_type object to generate the sequence.

Link : C++11 linear_congruential_engine

bernoulli_distribution bd;

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

bernoulli_distribution::< >param_type pt;

cout<< bd( lce , pt ) << ” ” << bd( dre , pt ) ;

Output in Code::blocks,

0   1

Property functions

param_type param() const; 

void param(const param_type& parm); 

result_type min() const; 

result_type max() const;

The param() functions returns the pram_type object.

bernoulli_distribution:: param_type pt ;
cout<< pt.p() << endl ;

bernoulli_distribution bd(0.9);

pt1=bd.param( );

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

Output in code::blocks,


The p() function simply gives the value of ‘p’ ,and as you can see the value of ‘p’ initially is 0.5 but assigning the return value of param() to pt1 changes the value of ‘p’ to 0.9.


This function sets the parameter set of the distribution,in other words we can say it changes the ‘p’ value of the param_type object.

bernoulli_distribution bd ;
cout<< bd.param( ).p( ) << endl ;

bernoulli_distribution:: param_type pt(23.45) ;

bd.param( pt );

cout<< bd.param( ).p( ) ;



After calling ‘bd.param( pt );’ the ‘p’ value is assign the ‘p’ value of ‘pt’.

min() function

This function gives the smallest value that can be generated by the distribution.

bernoulli_distribution bd ;
cout<< bd.min( ) ;



max() function

It gives the maximum value that can be generated by the distribution.

bernoulli_distribution bd ;
cout<< bd.max() ;



Side note

A bernoulli_distribution produces bool values ‘b’ distributed according to the discrete probability function,

C++11 bernoulli distribution probability