0

I need to generate roughly 700 uniformly distributed uint64 numbers on program startup. There are some external parts that rely on these numbers being the same every time the program is booted up. I could, of course, generate these numbers only once and then hardcode them into a large array, and use that. However, i would like to avoid that, if possible.

What i'm doing right now is having a fixed seed value and relying on the mt19937 generator to generate the exact same sequence every time.

    auto seed = 2079078781365792;
    std::mt19937_64 mt(seed);
    std::uniform_int_distribution<uint64_t> distribution;
    auto value = distribution(mt);

This works great for me now. But i'm wondering, if this is going to work with different compilers/different platforms, to always provide the user with the exact same sequence of numbers? Is it possible that the mersenne twister/uniform distribution is implemented slightly differently in such a way that would generate different sequence despite the same seed?

Jane Doe
  • 480
  • 3
  • 15
  • 1
    Does this answer your question? https://stackoverflow.com/questions/27492338/mersenne-twister-reproducibility-across-compilers – Pepijn Kramer Sep 04 '21 at 16:54
  • @PKramer I would very much like to avoid having a big file with random magical numbers, if i can achieve the exact same functionality with less than 10 lines of code. The link you provided answered my question perfectly, sorry i missed that before asking the question. Thank you! – Jane Doe Sep 04 '21 at 16:59
  • Maybe just running the twister by repeat calls to operator() will generate numbers that are random and evenly spread enough? That part should be portable. – Pepijn Kramer Sep 04 '21 at 17:01
  • Yes, i think that might suffice. I guess, I'll just run some tests and see. – Jane Doe Sep 04 '21 at 17:04
  • There's a difference between a uniform distribution and "uniformly distributed." A sequence of numbers such as created by `std::iota` that is shuffled is uniformly distributed but is not randomly distributed based against a uniform distribution function. The former will never have duplicates and the latter may. Depends on your needs. More info needed on your use case. – doug Sep 04 '21 at 17:38
  • I just realized, that the generator itself (`std::mt19937`) is already generating uniformly distributed numbers and `std::uniform_distribution` is only useful if we need numbers in a certain range. So i guess what i was doing originally didn't even make much sense in the first place. – Jane Doe Sep 04 '21 at 18:11

0 Answers0