Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ normal_distribution gives different results on different platforms

Tags:

random

c++11

This

std::mt19937 gen(123);
std::normal_distribution<> distr(0., .2);
printf("%f", distr(gen));

prints 0.339167 on my OSX 10.9 machine (built with clang 3.5) but prints -0.113922 on a linux box (gcc 4.8). I was expecting to see the same results everywhere.

When inspecting the underlying stream from mt19937, I see a consistent sequence of numbers on both platforms. So it appears that the platform inconsistency is in std::normal_distribution.

Is this expected behavior? Is there a way that I can set this up so that I am guaranteed to get the same results everywhere?

like image 213
Alex Flint Avatar asked Oct 30 '14 20:10

Alex Flint


1 Answers

std::mt19937 and siblings are very specific algorithms. The standard requires that e.g. The 10000th consecutive invocation of a default-constructed object of type mt19937 shall produce the value 4123659995. There's no wiggle room here.

std::normal_distribution and siblings, by contrast, are only required to produce results distributed according to a certain probability density function. There is no requirement for them to be any specific function.

like image 139
n. 1.8e9-where's-my-share m. Avatar answered Nov 19 '22 21:11

n. 1.8e9-where's-my-share m.