0

I tested the random number generator seeded with the same number, I am supposed to get the same result each time I run the program. And that's the case on the same computer. However, when I compile it on a different computer, I can get the same sequence repeatedly every time I run, but the results are different across computers. Is it caused by different implementation from the compiler of different version? I wonder if there is any library that is not dependent on the compiler or kernel or hardwares? I have tested rand(), mt19937, and default_random_engine. They all behaved the same.

srand (1);
cout<<rand()<<endl;
cout<<rand()<<endl;
cout<<rand()<<endl;
cout<<rand()<<endl;
cout<<rand()<<endl;

std::default_random_engine generator(1);

std::uniform_int_distribution<int> distribution(0,100000);
cout<<distribution(generator)<<endl;
cout<<distribution(generator)<<endl;
cout<<distribution(generator)<<endl;
cout<<distribution(generator)<<endl;
cout<<distribution(generator)<<endl;

std::random_device dev;
std::mt19937 rng(1);
std::uniform_int_distribution<std::mt19937::result_type> dist6(1,10000);
cout<<dist6(rng)<<endl;
cout<<dist6(rng)<<endl;
cout<<dist6(rng)<<endl;
cout<<dist6(rng)<<endl;
cout<<dist6(rng)<<endl;
exit(0);

The outputs are:

16807
282475249
1622650073
984943658
1144108930
48270
22497
73541
20860
47696
236
5193
906
7814
2896
Program ended with exit code: 0
weeo
  • 2,619
  • 5
  • 20
  • 29
  • 2
    Among its many sins, `rand` is [very weakly specified](https://en.cppreference.com/w/cpp/numeric/random/rand#Notes). Compilers do not all have to use the same implementation. This [classic xkcd joke](https://xkcd.com/221/) is too real to be optimally funny. – user4581301 Jan 24 '22 at 22:59
  • Use a PRNG from `` They will give the same results everywhere - note though, that if you use the standard _distributions_ they are not 100% the same everywhere. – Ted Lyngmo Jan 24 '22 at 23:01
  • Yes rand() was rarely same on different platforms, most of Boost.Random was OK, std:: is again garbage AFAIK. – Öö Tiib Jan 24 '22 at 23:03
  • @user17732522 Whell, then must assume they did something wrong. What two implementations did they try? [this example](https://godbolt.org/z/6donvch39) should print the same 10 random numbers on any platform unless I'm mistaken. – Ted Lyngmo Jan 24 '22 at 23:13
  • `std::mt19937` is specified to produce the same sequence of numbers for the same seed for all implementations. `std::uniform_int_distribution` is specified to produce a uniform distribution, but the details are not specified, and the result can differ for different implementations. – Pete Becker Jan 24 '22 at 23:20
  • Partial duplicate: [If we seed c++11 mt19937 as the same on different machines, will we get the same sequence of random numbers](https://stackoverflow.com/questions/48730363/if-we-seed-c11-mt19937-as-the-same-on-different-machines-will-we-get-the-same) – user17732522 Jan 24 '22 at 23:25
  • @TedLyngmo Many thanks! what library provide distributions that are cross platform? – weeo Jan 25 '22 at 00:16
  • @PeteBecker Thanks! That's the answer I need! Are you aware of nay distribution that can produce the same sequence across platforms? – weeo Jan 25 '22 at 00:17
  • @weeo I don't know of a separate library for distributions. I would create it myself and keep it as simple as possible and only do a version of `uniform_int_distribution` to start with. – Ted Lyngmo Jan 25 '22 at 05:54

0 Answers0