Why do I get the same sequence for every run with std::random_device with mingw gcc4.8.1?(为什么每次使用 std::random_device 和 mingw gcc4.8.1 运行时都会得到相同的序列?)
问题描述
我使用以下代码来测试 C++ 库.
I use the following code to test the C++ <random> library.
为什么每次运行编译后的可执行文件时我都会得到完全相同的序列?rd() 在编译时是确定性的吗?如何为每次运行获得不同的输出?
Why do I get the exact same sequence for every run of the compiled executable? Is rd() deterministic upon compilation? How do I get different output for each run?
Windows 7 64 位上的 GCC 4.8.1.使用 http://nuwen.net/mingw.html 中的 MinGW 分发版.
GCC 4.8.1 on Windows 7 64bit. Using MinGW distribution from http://nuwen.net/mingw.html.
我使用 Visual Studio 测试了相同的代码段.没有问题.输出是不确定的.这可能是我使用的 mingw gcc 4.8.1 中的一个错误.
I tested the same piece code with Visual Studio. There is no problem. The outputs are non deterministic. This could be a bug in mingw gcc 4.8.1 that I used.
#include <iostream>
#include <random>
using namespace std;
int main(){
random_device rd;
mt19937 mt(rd());
uniform_int_distribution<int> dist(0,99);
for (int i = 0; i< 16; ++i){
cout<<dist(mt)<<" ";
}
cout <<endl;
}
推荐答案
来自 http://en.cppreference.com/w/cpp/numeric/random/random_device:
请注意,如果非确定性源(例如硬件设备)对实现不可用,则 std::random_device 可以根据伪随机数引擎实现.
Note that std::random_device may be implemented in terms of a pseudo-random number engine if a non-deterministic source (e.g. a hardware device) is not available to the implementation.
我希望一个体面的实现至少可以为 RNG 播种.
I would expect a decent implementation to at least seed the RNG though.
我怀疑他们故意选择每次提供相同的序列,以表明流并不像承诺的那样随机.
I suspect they deliberately chose to deliver the same sequence each time, to make obvious the fact that the stream wasn't as random as promised.
这篇关于为什么每次使用 std::random_device 和 mingw gcc4.8.1 运行时都会得到相同的序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么每次使用 std::random_device 和 mingw gcc4.8.1 运
基础教程推荐
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- CString 到 char* 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
