I plan to create a challenge which will involve predictable randomness. I understand that for a given system, if srand()
is seeded using the same value, rand()
will return a predictable sequence of results. However I’m not sure to what extent this sequence is consistent between different systems. Will the result be the same as long as the compiler uses the same implementation? Or will the same program on two different systems yield different results?

- 2,543
- 4
- 16
- 32
-
2The `rand` functions on two different implementations may generate different sequences even if their initial seeds are the same. – Lxer Lx Jan 05 '20 at 14:39
-
But will, for example, a program compiled with the same compiler on multiple systems yield the same sequences? – clubby789 Jan 05 '20 at 14:40
-
If the same executable is used on multiple systems, I wouldn't expect different sequences, provided the generator is seeded with the same value. But if the program's source code is compiled on each of the systems, generating an executable per system, sequences would differ. Of course, I'm talking about standard library's `rand` here. You can implement your own algorithm to generate random numbers deterministically on multiple systems. – Lxer Lx Jan 05 '20 at 14:49
-
The standard library is in many cases *not* linked statically into the program so another version could be used – Antti Haapala -- Слава Україні Jan 05 '20 at 14:57
-
1@LxerLx: The same executable could link to a shared library, which could be different on different systems. – Eric Postpischil Jan 05 '20 at 15:20
-
1@EricPostpischil Rightly so. The same executable could link to a different shared library even _on the same system_, or `rand()` could be overridden on runtime using dynamic linker tricks such as `LD_PRELOAD=./my_rand.so ./a.out`. So a statically linked executable, which is rare nowadays, is necessary to expect the same sequence on different systems. – Lxer Lx Jan 05 '20 at 16:42
2 Answers
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;
}

- 40,708
- 1
- 95
- 119
To answer what you give in your question:
I’m not sure to what extent this sequence is consistent between different systems. Will the result be the same as long as the compiler uses the same implementation? Or will the same program on two different systems yield different results?
The C standard functions rand
and srand
use an unspecified algorithm. Which algorithm they use is determined not by the "compiler" or by the "system", but by the C standard library (e.g., glibc
or msvcrt*.dll
), including its version. For example, the same computer (using the same hardware and operating system) can have two programs that use a different implementation of the C standard library (e.g., glibc
versions X and Y, or glibc
and the imaginary lib_special_c
), which could implement rand
and srand
differently and could thus produce a different sequence of numbers for a given seed. On the other hand, the same program, running on two different computers, could dynamically link to a different C standard library implementation, and thus use a different implementation of rand
and srand
. Lxer Lx wrote the following in a comment:
The same executable could link to a different shared library [such as the C standard library] even on the same system, or
rand()
could be overridden on runtime using dynamic linker tricks such asLD_PRELOAD=./my_rand.so ./a.out
. So a statically linked executable, which is rare nowadays, is necessary to expect the same sequence [of numbers] on different systems.
However, for a given version of the C standard library, rand
requires the implementation to deliver the same sequence of numbers for a given seed.

- 32,158
- 14
- 82
- 96