Skip to content

Improved P_Random

tertu marybig requested to merge tertu/SRB2:p-random-sfc32 into next

Use the SFC32 RNG implemented in 2.2.12 for P_Random too.

What this means in practice:

  • As SFC32 has a 128-bit state, it is only reseeded at a few particular times.
    • At boot.
    • In single player, whenever G_InitNew is called with resetplayer = true. This typically means at the start of a new game.
    • In multiplayer, when a dedicated server becomes idle and when a server starts. (And of course when joining a netgame on the client.)
    • After demo playback or recording.
  • Random number quality improvements.
    • SFC32 is just a better generator than the sort-of-truncated Xorshift* in use right now.
    • P_RandomKey() and P_RandomRange() will no longer have restricted ranges (i.e. you can pass any INT32 to them and it will work fine) and use implementations designed to remove bias in the output.
    • When the generator is reseeded, it's reseeded with high-quality random data from a given OS source.
  • More shared code between P_Random and M_Random. In practice, this will only really be an advantage once the code needed to support the old RNG can be removed.

When demos recorded with demoversion 0x0012 or older are played back, the old implementation of P_Random will be used temporarily. Demoversion 0x0013 adds support for SFC32 states.

One concern I have is that buggy Lua code that calls P_RandomKey() and P_RandomRange() with different arguments may be more likely to desync.

Test checklist:

  • Single player
  • Record attack
  • Lump demos
  • Metal Sonic?
  • Netplay
Edited by tertu marybig

Merge request reports