C++11 random shuffle_order_engine


The C++11 random shuffle_order_engine adaptor produces the same random numbers that are produced by some base engine,but delivers them in a different sequence.The class declaration is shown below.

template<class Engine, size_t k >
class shuffle_order_engine;

The first parameter is the base engine,and the second parameter determine the size of the engine which is ‘size of the base engine plus k+1’.It is also used in by the engine’s algorithm to generate the random sequence.The value of ‘k’ shall hold the relation: 0 < k.

The class shuffle_order_engine types and member functions are given below.

Types and characteristics

typedef typename Engine::result_type result_type;
static constexpr size_t table_size = k;
static constexpr result_type min() { return Engine::min(); }
static constexpr result_type max() { return Engine::max(); }

The min() and max() function returns the minimum and maximum value the engine can produce.

Link : C++11 ranlux24_base predefined engine

Code example

shuffle_order_engine< ranlux24_base , 5 > soe ; //ranlux24_base is the base engine

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

Output in Code::Blocks,

0
16777215


Constructor

shuffle_order_engine();
explicit shuffle_order_engine(const Engine& e);
explicit shuffle_order_engine(Engine&& e);
explicit shuffle_order_engine(result_type s);
template<class Sseq> explicit shuffle_order_engine(Sseq& q);

 



Generating functions

result_type operator()()
void discard(unsigned long long z);

operator()

The random sequence of the shuffle_order_engine is accessed using the ‘operator()’ function.

Link :C++11 ranlux48_base predefined engine

Code example

shuffle_order_engine< ranlux48_base , 10 > soe ;

cout<< soe() ; //operator() called
cout<< endl << soe() ; //calling operator() again

Output in Code::blocks

55344035667239
28639057539807

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.

Code example

shuffle_order_engine< ranlux48_base , 10 > soe(9) , soe1(9) ; //soe and soe1 have the same initial state

//output five random number using soe object,the soe1 object will also give the same output
for(auto i=0 ; i<5 ; i++ )
{
cout<< soe() << ” ” ;
}

cout<< “\n\n” ;

cout<< soe1( ) << ” ” ; //access the random number of the first state
cout<< soe1( ) << endl ; //access the random number of the second state

soe1.discard( 1 ); //discard the third state and soe1 is set to 4th state

cout<< soe1( );

Output in Code::Blocks,

190964213869215   103201107228731   89431374096704   3747933582041   209737358661654

190964213869215   103201107228731
3747933582041


Property

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

The ‘base’ function returns a reference to the base engine.

Code example

shuffle_order_engine< default_random_engine , 3 > soeDRE(90) ;

default_random_engine dre;

dre=soeDRE.base( );

cout<< soeDRE() << endl ;
cout<< dre() << endl ;

Output in Code::Blocks,

282475249
1144108930


 


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