I'm wondering whether a code like this would produce, per standard, the same result on all platforms:
#include <random>
int main() {
std::mt19937 rng{1}; // constant seed
std::bernoulli_distribution distr{}; // default: p(0) = p(1) = 50%
int total = 0;
for(int i = 0; i < 100; i++)
total += distr(rng);
return total; // return value: 47 - or not?
}
I know that the uniform random number generators are required to be perfectly reproducible, one can either seed them with a known seed or operator<<
their internal state and operator>>
it in another run (be it or another machine, compiled with another compiler, etc.) to generate exactly the same sequence of pseudorandom numbers. But does the same hold for distributions?
In G++, the bernoulli_distribution
will use a single call to the rng
to generate a double
uniformly distributed between 0
and 1
, and compares that to 0.5
. But if someone, for example, redesigned the special case of equal probabilities to just extract a single bit from a stored value, and only make another call to the rng
when the stored entropy reaches zero, it would produce a different result in an otherwise identical environment.
Is it compliant with the standard to have different implementations of the distributions like this, or does it guarantee the number of calls to the URNG per a query on the distribution? I have certainly seen that they are permitted to have an internal state and G++ just does not use that. But that may be included for other purposes.
Aucun commentaire:
Enregistrer un commentaire