C++11 shuffle order engine random number generator
The C++11 shuffle order engine (or 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.
Link : C++11 random number generator
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.
shuffle_order_engine< ranlux24_base , 5 > soe ; //ranlux24_base is the base engine cout<< soe.min( ) << endl << soe.max( ) ;
Output in Code::Blocks,
16777215
Constructor
C++11 shuffle_order_engine has 5 constructors.
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
The generating function generate the random sequence.C++11 shuffle_order_engine has two generating sequence.
result_type operator()() void discard(unsigned long long z);
operator()
The random sequence of the shuffle_order_engine is accessed using the ‘operator()’ function.
shuffle_order_engine< ranlux48_base , 10 > soe ; cout<< soe() ; //operator() called cout<< endl << soe() ; //calling operator() again
Output in Code::blocks
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.
shuffle_order_engine< ranlux48_base , 10 > soe(9) , soe1(9) ; //soe and soe1 have the same initial state //Print five random number using soe object,soe1 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
3747933582041
Property
const Engine& base( ) const noexcept { return e; }
The ‘base’ function returns a reference to the base engine.
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,
1144108930