C++11 independent_bits_engine random number generator

The C++11 independent_bits_engine uses the random number produce by some engine to produce random numbers with a specified number of bits ‘w‘(refer to the class declaration).The class declaration is shown below.

Link : C++11 random number generator

template<class Engine , size_t w , class UIntType>
class independent_bits_engine;</strong>

The first parameter is the base engine using which the independent_bits_engine will produce a random numbers.
The second parameter ‘w’ is ‘unsigned int’ type and shall hold the relation : 0 < w and w <= numeric_limits<result_type>::digits.
The third parameter is the an ‘unsigned int’ type.

All of the types and class member functions of independent_bits_engine is given below.

Types and characteristics

typedef UIntType result_type;

static constexpr result_type min() { return 0; } 

static constexpr result_type max() { return 2<sup>w</sup>− 1; } 

Engine e; //private type

The min() and max() gives the minimum and maximum values the independent_bits_engine can generate.

independent_bits_engine<default_random_engine , 5 ,unsigned int> ibe ; //default_random_engine is the base engine

cout<< ibe.min() << endl
<< ibe.max() ;

independent_bits_engine<default_random_engine , 5 , float> ibe1 ; //error ,the third parameter type must be unsigned

Output in code::blocks,


The maximum value is 31 because the second parameter passed is 5 and a maximum value any 5 bits can represent is 31.



explicit independent_bits_engine(const Engine& e); 

explicit independent_bits_engine(Engine&& e); 

explicit independent_bits_engine(result_type s);

template<class Sseq> explicit independent_bits_engine(Sseq& q); 

Generating functions

The gnerating functions generate the random sequence.

result_type operator()( );

void discard(unsigned long long z);


The random number of the engine is obtained by calling the ‘operator()’ function.

independent_bits_engine< minstd_rand , 8 , unsigned int> ibemsr ;

cout<< ibemsr( ) << endl
<< ibemsr( ) ;

Output in code::blocks,


discard( unsigned long long z )

The ‘discard‘ function allows you to change the state of the engine.In calling this function the ‘z’ number of state of the engine from the current state is discarded and the engine will have the state set to the state next to the discarded state.

independent_bits_engine< minstd_rand , 8 , unsigned int> ibe1 , ibe2 ; //ibe1 and ibe2 have the same initial state so they will give the same random sequence

for(auto i=0 ; i<5 ; i++ )
cout<< ibe1() << ” ” ;

cout<< “\n\n “;
cout< ibe2() << ” “;
cout<< ibe2() <&lt endl ;

ibe2.discard( 2 ); //discards the 3rd and 4th state of the engine

cout<< ibe2() ;

Output in code::blocks,

142   225   69   124   240
142   225

Seed generating functions

void seed(result_type s = default_seed);

template&lt;class Sseq&gt; void seed(Sseq& q);//a templatized version of the above function

The seeding functions can set the engine to it’s initial state to reproduce the same random sequence if no argument is passed.It can also be used to change the state of the engine and obtain a new random sequence if an argument is passed.

independent_bits_engine<default_random_engine , 6 ,unsigned int> ibe;

for(auto i=0; i<4 ; i++ )
cout<< ibe( ) << ” ” ;

cout<< “\n\n” ;
ibe.seed() ; //argument is not passed,set the engine to the initial state

cout<< ibe() << ” ” ;
cout<< ibe() ;

Output in code::blocks,

38 48 24 41

38 48


const Engine& base() const noexcept { return e; };

The ‘base’ function return the current state of the base engine.

independent_bits_engine<default_random_engine , 6 , unsigned int> ibe(45) ;

cout<< ibe() << endl ;
default_random_engine dre ;

dre=ibe.base(); //dre and ibe now have the same state

cout<< ibe() << endl ;
cout<< dre() ;

Output in code::blocks


After calling the base() function since ibe and dre have the same state they give the same output.