-1

Here is my code. It suppose to generate normal_distribution.

#include <iostream>
#include <random>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/normal_distribution.hpp>


int main(int argc, const char *argv[])
{

    // Initialize seed .
    std::mt19937 gena;
    boost::random::mt19937 genb;
    gena.seed( 10 );
    genb.seed( 10 );

    std::normal_distribution<> norma;
    boost::random::normal_distribution<> normb;

    std::cout << "STD BOOST" << std::endl;
    for (size_t i = 0; i < 20; i++) 
        std::cout << norma(gena) << ' ' << normb(genb) << std::endl;

    return 0;
}

I compile it with gcc-8.1 on arch-linux, and I get the following:

STD BOOST -0.0512656 1.16196 -1.90693 -0.742973 1.23919 -1.10165 -0.212754 1.03149 -0.376303 0.597194 0.0660327 -0.12573 -1.01706 1.10728 0.907624 -0.370934 -0.682124 -0.404068 -0.461218 -0.46376 0.965666 -0.607265 0.560664 -1.44186 0.749932 0.449968 -0.31456 -0.268649 0.366249 0.986499 0.608089 -0.500526 -1.08684 -0.0215645 0.120559 -0.805144 1.40203 0.390409 -0.434259 0.0991071

I use the same code on a Mac machine (Apple LLVM version 7.0.2 (clang-700.1.81)); and I get the following:

STD BOOST -1.90693 1.16196 -0.0512656 -0.742973 -0.212754 -1.10165 1.23919 1.03149 0.0660327 0.597194 -0.376303 -0.12573 0.907624 1.10728 -1.01706 -0.370934 -0.461218 -0.404068 -0.682124 -0.46376 0.560664 -0.607265 0.965666 -1.44186 -0.31456 0.449968 0.749932 -0.268649 0.608089 0.986499 0.366249 -0.500526 0.120559 -0.0215645 -1.08684 -0.805144 -0.434259 0.390409 1.40203 0.0991071

Note the first column which uses std::random is different; but if you look closely, first column have many elements common but seem to be shuffled.

I am worried after one test broke on Travis (that means, Ububtu-14.04 and xcode-9.2 will show the same results) after we replaced our old normal distribution generator in favor of std::random.

Is it a bug? Or I am doing something wrong? BOOST is doing OK.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
Dilawar
  • 5,438
  • 9
  • 45
  • 58
  • IIRC, `std::*_distribution` are implementation-dependent, but random engines themselves should produce the same result. – HolyBlackCat Aug 04 '18 at 12:54
  • @HolyBlackCat Indeed. rng's are producing same sequence. In fact, `uniform_distribution` is also producing same sequence. – Dilawar Aug 04 '18 at 12:57
  • It's a duplicate of this one https://stackoverflow.com/questions/34903356/c11-random-number-distributions-are-not-consistent-across-platforms-what-al – Dilawar Aug 04 '18 at 13:17
  • 1
    When doing tests, you should stub in an RNG that you know will produce the same results every time. Otherwise, you will get test failures. – David Schwartz Aug 04 '18 at 13:22

1 Answers1

3

When you use different compilers (or even versions of compilers) the pseudo random numbers in the normal distribution aren't obliged to be the same just because you've used the same seed. I wouldn't rely on the sequence being the same unless I was using the same compiler, compiler version and possibly optimisation settings - check the compiler implementation documentation if you've reason to care.

It's unsurprising that boost is more consistent because you're presumably using the same implementation despite the compiler difference.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • 1
    The sequences produced by **generators** with the same seed are required to be the same on all implementations. The sequences produced by feeding the output from a generator to a **distribution** are not required to be the same even if the generators produce the same output sequence. – Pete Becker Aug 04 '18 at 13:27
  • @PeteBecker: quite so - perhaps my typo "in the distribute" rather than "distribution" makes it less clear that's what I'm saying (now fixed). I also provided the link to the duplicate Q where answers emphasise that distinction (though someone deleted my comment about the duplicate when πάντα ῥεῖ closed this as a duplicate). – Tony Delroy Aug 04 '18 at 13:30
  • The wording is very confusing. "pseudo random numbers in the distribution"... I took that to mean the compiler distribution. It sounds like the random number generators are allowed to give different sequences for different seeds. – Galik Aug 04 '18 at 13:53
  • @Galik: Oh :-/. I'll qualify distribution with "normal" then. Cheers – Tony Delroy Aug 04 '18 at 14:02