How predictable is the result of rand() between individual systems?

A random number generator (RNG) is not much code if you simply want good statistical properties without any cryptographic concerns. Include the code for the RNG in your program. Then it will be the same sequence wherever it’s run.

Consider something from the PCG family or Xoshiro. M.E. O’Neill’s blog has several posts on small RNGs that pass PractRand and TestU01 statistical tests, like Bob Jenkins’s Small and 64-bit Minimal Standard generators — these are just a few lines of code! Here’s an example :

uint128_t state = 1;   // can be seeded to any odd number

uint64_t next()
{
    state *= 0x0fc94e3bf4e9ab32866458cd56f5e605;
            // Spectral test: M8 = 0.71005, M16 = 0.66094, M24 = 0.61455
    return state >> 64;
}

Leave a Comment