c++11 - Random Number Order in C++ using <random> -


i have following code, wrote test part of 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 2 generators seeded same value, , therefore expected second row in output identical first one. instead, output is

1 1 3 1 3 1 

the state of 2 generators printed in *.dat files same. wondering if there might hidden multi-threading in random number generation causing order mismatch.

i compiled g++ version 5.3.0, on linux, flag -std=c++11.

thanks in advance help.

x << y syntactic sugar function call operator<<(x, y).

you remember c++ standard places no restriction on order of evaluation of arguments of function call.

so compiler free emit code computes x first or y first.

from standard: §5 note 2:

operators can overloaded, is, given meaning when applied expressions of class type (clause 9) or enumeration type (7.2). uses of overloaded operators transformed function calls described in 13.5. overloaded operators obey rules syntax specified in clause 5, requirements of operand type, value category, and evaluation order replaced rules function call.


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -