問題描述
是否有一些類似于 srand()
的函數(shù),我需要調(diào)用它來確保 std::random_shuffle()
總是產(chǎn)生不同的結(jié)果?即如果我用相同的數(shù)據(jù)多次調(diào)用它,我希望每次的順序都不同.我如何確保這一點(diǎn)?
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
有兩種形式.一個(gè)接受 2 個(gè)參數(shù)(開始/結(jié)束迭代器),一個(gè)接受 3 個(gè)參數(shù)(開始/結(jié)束迭代器和一個(gè)隨機(jī)生成器).
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()
為其隨機(jī)數(shù)生成器提供種子.您也可以使用 3 參數(shù)版本并自己提供 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.
這篇關(guān)于如何確保 std::random_shuffle 總是產(chǎn)生不同的結(jié)果?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!