如何确保std :: random_shuffle总是产生不同的结果? [英] How to make sure that std::random_shuffle always produces a different result?
问题描述
有一些函数,类似 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个参数(begin / end迭代器),一个需要3(begin / end迭代器和随机生成器)。
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总是产生不同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!