mardi 26 mars 2019

In C++, why can't I generate independent random integer samples using two default random engines

I want to have to independent random distributions of integers in a configurable range. What I had originally is illustrated by the following program:

#include <random>
#include <cstdio>
#include <cstdlib>
using namespace std;

int main(int argc, char* argv[])
{
    default_random_engine generator1;
    default_random_engine generator2;
    uniform_int_distribution<int> dist1(0,atoi(argv[1]));
    uniform_int_distribution<int> dist2(0,atoi(argv[2]));

    generator1.seed(0);
    generator2.seed(1);
    for (int i = 0; i < 60; i++)
        printf("(%d, %d)\n", dist1(generator1), dist2(generator2));
    return 0;
}

This turns out to always generate equal values when argv[1] and argv[2] are equal, and has less obvious dependencies when they are different as well. For in case I used different engine instances, and even seeded them differently.

What is going on here? I noticed that the problem goes away if I replace the default_random_engine by the mt19937, but that is something I never would have guessed. Also, is the other engine supposed to be able to produce independent samples?




Aucun commentaire:

Enregistrer un commentaire