简体   繁体   中英

C++11 multiple random number engine adaptors

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;
}

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM