Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 multiple random number engine adaptors

Tags:

c++

random

c++11

Is it possible to use a random engine provided by the STL in C++11 with multiple adaptors simultaneously?

For example, using the Mersenne Twister Engine with both the Discard Block engine adaptor (from each block of size P generated by the base engine, the adaptor keeps only R numbers, discarding the rest) and the Shuffle Order engine adaptor (delivers the output of a random number engine in different order).

Example engine adaptor use for anyone unaware:

//some code here to create a valid seed sequence
mt19937 eng(mySeedSequence);
discard_block_engine<mt19937,11,5> discardWrapper(eng);
shuffle_order_engine<mt19937,50> shuffleWrapper(eng);

for (int i=0; i<100; ++i) {
  //for every 5 calls to "discardWrapper()", the twister engine 
  //advances by 11 states (6 random numbers are thrown away)
  cout << discardWrapper() << endl;
}

for (int i=0; i<100; ++i) {
  //essentially 50 random numbers are generated from the Twister
  //engine and put into a maintained table, one is then picked from
  //the table, not necessarily in the order you would expect if you
  //knew the internal state of the engine
  cout << shuffleWrapper() << endl;
}
like image 245
Victor Stone Avatar asked Mar 11 '26 21:03

Victor Stone


1 Answers

Yes, you can do that. You just need to define one adaptor type in terms of the other:

typedef std::discard_block_engine<std::mt19937, 11, 5> discard_engine_t;
typedef std::shuffle_order_engine<discard_engine_t, 50> shuffle_engine_t;

std::mt19937 mt_eng;
discard_engine_t discard_eng(mt_eng);
shuffle_engine_t shuffle_eng(discard_eng);
like image 93
Benjamin Lindley Avatar answered Mar 15 '26 15:03

Benjamin Lindley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!