std::uniform_int_distributionaccepts any of <random> PRNGs, including those that are consistent between implementations and platforms.
However, std::uniform_int_distributionit seems itself incompatible between implementations, so I can’t rely on the possibility of duplicating them, even using a common PRNG and seed. It also affects dependent functions, for example. std::shuffle().
So for example:
#include <random>
#include <iostream>
#include <string>
#include <algorithm>
template<typename T>
void printvector(const std::string& title, const std::vector<T>& v)
{
std::cout << title << ": { ";
for (const auto& val : v) { std::cout<<val<<" "; }
std::cout << "}" << std::endl;
}
int main()
{
const static size_t SEED = 770;
std::minstd_rand r1(SEED), r2(SEED), r3(SEED);
std::vector<int> vPRNG;
for (int i=0; i<10; ++i) { vPRNG.push_back((int)r1()); }
std::vector<size_t> vUniform;
std::uniform_int_distribution<int> D(0,301);
for (int i=0; i<10; ++i) { vUniform.push_back(D(r2)); }
std::vector<size_t> vShuffled {1,2,3,4,5,6,7,8,9,10};
std::shuffle(vShuffled.begin(), vShuffled.end(), r3);
printvector("PRNG", vPRNG);
printvector("UniformDist", vUniform);
printvector("Shuffled", vShuffled);
}
It gives different results in different systems, although the PRNG itself generates exactly the same numbers:
System 1:
PRNG: { 37168670 1020024325 89133659 1161108648 699844555 131263448 1141139758 1001712868 940055376 1083593786 }
UniformDist: { 5 143 12 163 98 18 160 140 132 152 }
Shuffled: { 7 6 5 2 10 3 4 1 8 9 }
System 2:
PRNG: { 37168670 1020024325 89133659 1161108648 699844555 131263448 1141139758 1001712868 940055376 1083593786 }
UniformDist: { 19 298 170 22 53 7 43 67 96 255 }
Shuffled: { 3 7 4 1 5 2 6 9 10 8 }
How can I correctly implement a uniform distribution compatible between different platforms and standard library implementations?