为什么我得到相同的顺序,每次运行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?

查看:1426
本文介绍了为什么我得到相同的顺序,每次运行std :: random_device与mingw gcc4.8.1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码测试 c ++< random> 库。

I use the following code to test 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?

GCC 4.8.1在Windows 7 64位上。使用 http://nuwen.net/mingw.html 中的MinGW分发

GCC 4.8.1 on Windows 7 64bit. Using MinGW distribution from http://nuwen.net/mingw.html

EDIT:我使用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 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\">http://en.cppreference.com/w/cpp/numeric/random/random_device :

From 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天全站免登陆