为什么每次使用 std::random_device 和 mingw gcc4.8.1 运行时都会得到相同的序列? [英] Why do I get the same sequence for every run with std::random_device with mingw gcc4.8.1?

查看:49
本文介绍了为什么每次使用 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 运行时都会得到相同的序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆