Closed psteinb closed 5 years ago
The PRNGs (MT and PCG) should produce the same numbers on any platform. However, the algorithms for producing the standard random number distributions in C++ are implementation-defined. You could use (a fixed version of) boost.random
to achieve cross-platform reporducable random draws.
The odd pattern for the normal draws comes from the used algorithm. Both platforms use the (rather slow) Box-Muller method. This transforms two random numbers from [0,1) to two N(0,1) numbers. Typically one will cache one result and return the other.
BTW, there is no need to have two instances of the RNG here.
@rstub Interesting & thanks for the explanation. I didn't dig the c++ standard, but this is somewhat discouraging (at least for my use case). At this point, adding boost.random as a dependency appears too heavy for the project at hand. I'll have to find an alternative.
I checked with python and this gives the same sequence on both platforms in question. As a whole, that is quite discouraging with respect to c++. Not sure about other languages.
Not fixing the distributions is indeed a major problem with random
. However, much of the complexity in both random
and boost.random
stems from them being compatible with virtually any PRNG. It shouldn’t be to difficult to roll your own If you restrict yourself to sensible ones (i.e. returning 32bit or 64Bit ints) and a small set of distributions. See http://www.pcg-random.org/posts/bounded-rands.html for uniform_int
examples.
The uniform distribution call to (0,50) is unnecessary. The pcg library already supports the generation of integers over an interval. The reference offered by @rstub is excellent, and note that the code is super simple.
I must apologize if this issue reveals a misunderstanding on my side. Nevertheless, I've been banging my head against this for too long, so I'd like to go out public and ask for help.
I am working on a simulation code that is stochastic in nature, so requires drawing from PRNGs repeatedly. In an attempt to move forward in a tested and reproducible way, I discovered a problem with xcode recently, that I have boiled down to some simple example code:
or using pcg of commit b656278
The interesting observation is, that the above code gives different results with
gcc 8.3.1
on fedora 29 and on osx with xcode9.0.0, Target: x86_64-apple-darwin17.7.0
. Here is a diff of the top 10 numbers using pcg:And using the stdlib:
The normal distribution (left column) appears to show consistent numbers but in an arbitrary ordering when comparing linux and osx. About the integer numbers I am quite uncertain what is going on. The question for me now is, is this an expected behavior/feature of PRNGs or is this a bug in xcode/gcc ?