簡體   English   中英

C ++ 11多個隨機數引擎適配器

[英]C++11 multiple random number engine adaptors

是否可以同時使用C ++ 11中STL提供的隨機引擎和多個適配器?

例如,使用具有Discard Block引擎適配器的Mersenne Twister引擎(來自基本引擎生成的每個P大小塊,適配器僅保留R號,丟棄其余的)和Shuffle Order引擎適配器 (提供輸出隨機數引擎的順序不同)。

示例引擎適配器用於任何不知道的人:

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

是的,你可以這么做。 您只需要根據另一個定義一個適配器類型:

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