c++ - 为什么每次使用 std::random_device 和 mingw gcc4.8.1 运行都会得到相同的序列?

标签 c++ c++11 random stl

我使用以下代码测试 C++ <random>图书馆。

为什么每次运行已编译的可执行文件都会得到完全相同的序列?是 rd()编译时确定性?如何为每次运行获得不同的输出?

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

编辑:我用 Visual Studio 测试了相同的代码。没有问题。输出是不确定的。这可能是我使用的 mingw gcc 4.8.1 中的一个错误。

#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 :

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 播种。

编辑:我怀疑他们故意选择每次都提供相同的序列,以表明流并不像 promise 的那样随机。

关于c++ - 为什么每次使用 std::random_device 和 mingw gcc4.8.1 运行都会得到相同的序列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61282593/

相关文章:

c++ - CMake如何配置这个C++项目?

C++ - 指向函数的全局指针

c++ - vector<unique_ptr> 带来麻烦

java - 对于列表中的每个元素,选择该元素和 3 个随机非重复元素

c++ - std::list iterator.erase() 导致无效指针

c++ - 模板元编程 NamedPipe 客户端服务器

c++ - "Using"声明范围仅在当前类?

MySQL简单行数增量计数器

c++ - 存储和重新加载随机数生成器状态

c++ - 如何二进制读取 7z 存档