Skip to content

New M_Random implementation

tertu marybig requested to merge tertu/SRB2:m-random-take2 into next

List of changes

  • M_Random functions now uses a specific PRNG algorithm, SFC32, which can produce 32 bits at a time and is both fast and good quality.
  • M_RandomFixed no longer tries to "compensate for insufficient randomness", as the conditions it was compensating for are no longer possible.
  • M_RandomKey and M_RandomRange no longer use floating point math and can return any valid INT32. They also have much lower bias than they used to.
  • v.RandomRange and v.RandomKey no longer warn on arguments over 65536, as the reason they did so was linked to the problems with M_RandomKey and M_RandomRange.
  • SRB2 will use a high-quality random number source (CryptGenRandom on Windows, /dev/urandom or /dev/random on Unix and Android) to seed the M_Random PRNG by default. If it can't, it will fall back on the system time.
  • M_RandomizedSeed will generate a new seed if it generates a 0, making each valid P_Random seed equally likely. It also no longer depends on the frame count.
  • The P_Random PRNG is now seeded when SRB2 starts, so code that wants to use random numbers before the game has been started for the first time should no longer need to use workarounds for poor random number quality at that point.
Edited by tertu marybig

Merge request reports