简体   繁体   English

为什么 std::uniform_int_distribution 在不同系统中没有给出相同的答案?

[英]Why does std::uniform_int_distribution not give the same answer across different systems?

Code example:代码示例:

#include <iostream>
#include <random>
using namespace std;

int main()
{
    mt19937 generator(0);
    uniform_int_distribution<int> distr(0, 100);

    for(auto i = 0; i < 10; ++i)
    {
         cout << distr(generator) << "\n";
    }
}

This code produces different numbers on Mac and Windows, but replacing uniform_int_distribution with uniform_real_distribution fixes that issue, and sequence generated is the same on both platforms.此代码在 Mac 和 Windows 上生成不同的数字,但将uniform_int_distribution替换为uniform_real_distribution修复了该问题,并且生成的序列在两个平台上是相同的。

Why does this happen?为什么会发生这种情况?

The algorithm for distributions is not specified, different implementations may produce different results.分布的算法没有指定,不同的实现可能会产生不同的结果。

(The only thing the standard does specify are the raw random bit engines; those produce predictable results.) (标准唯一指定的是原始随机位引擎;那些产生可预测的结果。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM