简体   繁体   English

C ++ 11多个随机数引擎适配器

[英]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? 是否可以同时使用C ++ 11中STL提供的随机引擎和多个适配器?

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). 例如,使用具有Discard Block引擎适配器的Mersenne Twister引擎(来自基本引擎生成的每个P大小块,适配器仅保留R号,丢弃其余的)和Shuffle 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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