sandyre
sandyre

Reputation: 175

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.

Why does this happen?

Upvotes: 1

Views: 442

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 477070

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.)

Upvotes: 5

Related Questions