jeudi 28 janvier 2016

Random Number Order in C++ using

I have the following code, that I wrote to test a part of a larger program :

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

int main()
{
  mt19937_64 Generator(12187) ;
  mt19937_64 Generator2(12187) ;
  uniform_int_distribution<int> D1(1,6) ;

  cout << D1(Generator) << " " ;
  cout << D1(Generator) << " " << D1(Generator) << endl ;
  cout << D1(Generator2) << " " << D1(Generator2) << " " << D1(Generator2) << endl ;

  ofstream g1("g1.dat") ;
  g1 << Generator ;
  g1.close() ;
  ofstream g2("g2.dat") ;
  g2 << Generator2 ;
  g2.close() ;
}                                                            

The two generators are seeded with the same value, and therefore I expected the second row in the output to be identical to the first one. Instead, the output is

1 1 3
1 3 1

The state of the two generators as printed in the *.dat files is the same. I was wondering if there might be some hidden multi-threading in the random number generation causing the order mismatch.

I compiled with g++ version 5.3.0, on Linux, with the flag -std=c++11.

Thanks in advance for your help.

Aucun commentaire:

Enregistrer un commentaire