C++11 discard_block_engine random number generator

The C++11 random discard_block_engine is one of the engine adapters (the engine which uses another engine to produce a random sequence having totally different properties) found in C++11 random number generator engine.The engine which the discard_block_engine depends on to produce the random number is known as the base engine,you can choose any of the basic engine or any of the engine with predefined parameters as the base engine.

Link : C++11 random number generator

The declaration of this engine is shown below.

template<class Engine, size_t p, size_t r>
class discard_block_engine; 

The discard_block_engine is an adaptor engine so it requires another engine to generate random sequence.In the declaration above you can see the first parameter is of engine type.So to make discard_block_engine workable you have to pass another engine as the first argument.

While passing the parameters the following relation shall hold,
0 < r
r <= p

The discard_block_engine class types and member functions are shown below.

Types and characteristics

typedef typename Engine::result_type result_type;

static constexpr size_t block_size = p;

static constexpr size_t used_block = r;

static constexpr result_type min() { return Engine::min(); }

static constexpr result_type max() { return Engine::max(); }

Engine e; //private type

int n; //private type

The min( ) and max( ) function gives the minimum and maximum value of the engine.

discard_block_engine<default_random_engine , 10 , 5 > dbe ; //passing implementation-defined engine

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

discard_block_engine<minstd_rand0 , 90 , 200 > dbe1 ; //error! r>p

discard_block_engine<subtract_with_carry_engine<unsigned int , numeric_limits<unsigned int>::digits , 100 , 300> swce ; //work fine,passing basic engine

Output in code:;blocks,



discard_block_engine(); //Default constructor 

explicit discard_block_engine(const Engine& e); //Constructor accepting a seed

explicit discard_block_engine(Engine&& e); 

explicit discard_block_engine(result_type s); 

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

Generating function

result_type operator()( );

void discard(unsigned long long z);


The ‘operator()’ is used to get the random number of the sequence generated by the discard_block_engine.

discard_block_engine<minstd_rand0 , 10 , 5 > dbe;

cout<< dbe() ;  //get random number

Output in code::blocks,


discard(unsigned long long z)

The ‘discard’ function can alter the state of the engine by jumping the state of the engine next to the state after adding ‘z’ to the current state,confused? look at the code example below.

discard_block_engine<minstd_rand0 , 10 , 5 > dbe1 , dbe2 ; //dbe1 and dbe2 have the same initial state so they will generate the same random sequence

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

cout<< dbe2() << ” ” ; //getting random number of 1st state
cout<< dbe2() << “\n”; //getting random number of 2nd state

dbe2.discard( 1 ); //jump the 3rd state of the engine and set the engine to the 4th state

cout<< dbe2() <&lt ; //access the random number of the 4th state
cout<< dbe2() <&lt ; //access the random number of the 5th state

Output in Code::Blocks ,

16807   282475249   1622650073   984943658   1144108930
16807   282475249
984943658   1144108930

Seed generating functions

void seed(result_type s = default_seed);

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

These functions can change the state of the engine by passing a seed value.Passing a seed is necessary if you want to divert from the current sate of the engine and generate a totally different random sequence.

discard_block_engine<minstd_rand0 , 10 , 5 > dbe;
cout<< dbe() << endl ; //get random number of the first state

dbe.seed( 230 );

cout<< dbe( ) << endl ; 

Output in Code::blocks,


Calling ‘seed’ without passing any argument will set the engine to it’s initial state.


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

The ‘base’ function get the reference of the underlying engine object.You can use the return type of this function to assign to the object of the base engine.

discard_block_engine<minstd_rand0 , 10 , 5 > dbe(56) ; //set the engine to some state
minstd_rand0 ms ;

ms=dbe.base(); //set the same engine state as the dbe

cout<< dbe() << endl ;
cout<< ms() << endl ;

Output in Code::blocks,


They give the same value since they have the state.