2015-05-03 29 views
1

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

例如,使用梅森倍捻機引擎與兩個Discard Block engine adaptor(從由基座發動機產生的大小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; 
} 
+0

您的意思是說如果可以改編自適應引擎嗎?或者,如果可以通過兩種不同的方式調整相同的底層引擎輸出? –

+0

本質上也是。要同時具​​有2個適配器的引擎。 –

回答

3

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

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

好的,完美的。非常感謝! –