7

I'm generating a sequence of random numbers with std::mt19937_64. I've noticed that, when run with GCC and Clang on the same platform with the same seed, I obtain a different sequence. I've run the program through Valgrind and found no uninitialized memory.

Is there any guarantee to reproducibility across compilers or across platforms with std::mt19937_64?

Edit: Running with std::normal_distribution

  • 3
    If you need cross-compiler guaranteed compatibility you can use [Boost.Random](http://www.boost.org/doc/libs/1_57_0/doc/html/boost_random.html). – Mark Ransom Dec 15 '14 at 20:16
  • Perhaps I'm a bit naive here, but arguably if they are using the same algorithm and same parameters, shouldn't they reproduce the same sequence? – Rufflewind Dec 15 '14 at 20:19
  • 3
    Since you are using normal distribution see my [answer here](http://stackoverflow.com/a/24554535/1708801) basically the answer is no but the underlying engines should be consistent looks like a duplicate. – Shafik Yaghmour Dec 15 '14 at 20:26

1 Answers1

10

The numbers that engines generate are guaranteed to be reproducible across implementations, but the distributions are not. (source: rand() considered harmful).

The N3337 standard draft says this about normal_distribution (26.5.8.5.1):

A normal_distribution random number distribution produces random numbers x distributed according to the probability density function

enter image description here

The distribution parameters µ and σ are also known as this distribution’s mean and standard deviation

And... that's it. It doesn't specify the order of the generated numbers, nor algorithm, nor example outputs.

The standard is very elaborate about mersenne_twister_engine (26.5.3.2), it specifies the state transition function, initial seeding algorithm and so on.

Community
  • 1
  • 1
milleniumbug
  • 15,379
  • 3
  • 47
  • 71