The 'time.tm_year * 86400 * 366' line caused the result to overflow
what can be stored in an int; and signed int overflow is undefined
behaviour. The result goes into an unsigned int anyway, so now
all the intermediate computations are also done with unsigned int.
It still overflows (not on this line, but on the next one), but
that is fine as the standard guarantees that unsigned int overflow
wraps around.
The old RNG method had non-standard periods, ranging from some seeds looping on themselves (seed = 1184201285) to some seeds having periods as low as 11 or 48, as listed in https://github.com/scummvm/scummvm/pull/3340. This is a problem even for games that run the RNG once a frame, as the possibilities for random events is greatly reduced should the initial seed be in one of these sets of small periods.
Xorshift* is a standard, fast, non-cryptographic PRNG with academic backing that has period 2^32-1 (all seeds lead to another seed except 0, which is excluded from the initial seeds). Many different flavors are possible, as listed in the paper, but the choice implemented in this pull request uses only a single 32-bit integer as a state, like the old PRNG.
Co-authored-by: Thierry Crozat <criezy@scummvm.org>
Co-authored-by: Filippos Karapetis <bluegr@gmail.com>
This improves the range of seeds compared to using only getMillis (which
is the number of milliseconds since program start) as the seed. This was
especially an issue if you started the game directly from command line,
Steam, or GOG instead of using the ScummVM interface. Previously the
initial seed was just the load time, which can have very small variance
on a fast computer.
Calling RandomSource::getRandomNumber() results in divide by zero
exception when called with max = 4294967295 as max + 1 overflows to zero
and the subsequent modulus operation causes the exception.
Certain game saves in Bladerunner trigger this.
This also removes the dependency of engines on the event recorder header
and API, and will make it easier to RandomSources that are not properly
registered.