How to make sure that std::random_shuffle always produces a different result?(如何确保 std::random_shuffle 总是产生不同的结果?)
问题描述
是否有一些类似于 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 总是产生不同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何确保 std::random_shuffle 总是产生不同的结果?
基础教程推荐
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- CString 到 char* 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
