問題描述
是否有一些類似于 srand()
的函數,我需要調用它來確保 std::random_shuffle()
總是產生不同的結果?即如果我用相同的數據多次調用它,我希望每次的順序都不同.我如何確保這一點?
Is there some function, similar to srand()
, that I need to call to make sure that std::random_shuffle()
always produces different results? i.e. if I call it several times with the same data, I want the order to be different every time. How can I make sure of that?
推薦答案
std::random_shuffle
有兩種形式.一個接受 2 個參數(開始/結束迭代器),一個接受 3 個參數(開始/結束迭代器和一個隨機生成器).
std::random_shuffle
has two forms. One that takes 2 arguments (begin/end iterators), and one that takes 3 (begin/end iterator and a random generator).
第一種形式使用 std::rand()
,因此您將使用 std::srand()
為其隨機數生成器提供種子.您也可以使用 3 參數版本并自己提供 RNG.
The first form uses std::rand()
, so you would use std::srand()
to seed it's random number generator. You can also use the 3-argument version and provide the RNG yourself.
這篇關于如何確保 std::random_shuffle 總是產生不同的結果?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!