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

时间:2015-05-03 22:23:34

标签: c++ c++11 random

是否可以使用C ++ 11中STL提供的随机引擎同时使用多个适配器?

例如,使用具有Discard Block engine adaptor的Mersenne Twister Engine(来自基本引擎生成的每个P大小块,适配器仅保留R号,丢弃其余的)和Shuffle Order engine adaptor (以不同的顺序提供随机数引擎的输出)。

示例引擎适配器用于任何不知道的人:

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

1 个答案:

答案 0 :(得分:3)

是的,你可以这样做。您只需要根据另一个定义一个适配器类型:

X-Forwarded-Host