[Open Assets] SeedRng: seedable pseudorandom number generator

This content may be freely modified and/or maintained by anyone.
Status
Not open for further replies.

tertu

cheap spirit
SeedRng is an alternative RNG implementation that grew out of a different project I was working on. It might be better for some uses, maybe.

How to Use
One global function is created: A_SeedRng_Get(). This function returns a table that contains all of the functions SeedRng includes (which is what SeedRng means in the following function definitions).
SeedRng.Create(net, <seed,...>): Creates a state, which is needed for all other functions. If net is true, this state is network-synchronized. If net is false, it is not. seed is 0-4 numbers, which are used to fill the state. Any missing numbers are filled in. It returns the state, which you should treat as an opaque object (in reality, if networked it is a number and if not it is a table).
SeedRng.Get32(state), SeedRng.GetFixed(state), SeedRng.GetByte(state), SeedRng.GetSignedByte(state), SeedRng.GetBool(state): These all take one argument, a state, and return the type of value their name indicates.
SeedRng.GetChance(state, chance): Basically the same as P_RandomChance().
SeedRng.GetKey(state, limit): Returns a random integer between 0 and limit-1. limit can be as large as INT32_MAX.
SeedRng.GetRange(state, lower, upper): Returns a random integer between lower and upper, as long as they are within INT32_MAX-1 of each other. If not, an error occurs.
SeedRng.Destroy(state): Destroys a state. Trying to use it again after this will cause some sort of error. You should do this whenever you are done with a state (but for non-networked ones it doesn't really matter).

Commands
If you use this script, you should probably remove the commands.
They are not intended for any use but testing.
srngtest_init: Creates a state for the out commands.
srngtest_netinit: Creates a networked state for the out commands. There's no way to destroy it later, so this one really shouldn't be used while you're doing anything else.
srngtest_out32: Runs Get32 as listed above on the state made by the init commands and then prints it with some other nonsense on the console.
srngtest_out1k: Similar, except it tests GetKey instead.
srngtest_demo: Sets up the demonstration mode shown in the screenshots.

Network-synchronized vs. unsynchronized states
Network-synchronized states must be used for any behavior that must be the same on all instances of the game, but take up space in $$$.sav. For that reason, if an effect is purely visual, you should probably use an unsynchronized state.
SeedRng r1 lets you use network-synchronized states from HUD code. This is unsafe and should not be done. If using r1a, you will get an error if you do this. And no, it is never a good idea to do: it will likely result in desyncing.

Acknowledgements can be found in the source code.

Possible issues
I am somewhat uncertain about the quality of the left and right shift functions, but not very.

Changelog
r2
  • More comprehensive HUD checks.
  • Better seed checking.
  • Some fixes to the demo functions.
r1a
  • Rotate function reimplemented to not call any functions
  • HUD checks implemented, preventing unsafe function calls during HUD drawing.
r1
Initial release.

Planned updates

None as of right now. Any bugs will of course be fixed, but I don't know of any right now.
 

Attachments

  • srng_still.png
    srng_still.png
    60.9 KB · Views: 547
  • srng_motion.gif
    srng_motion.gif
    1.4 MB · Views: 591
  • SeedRng-r1a.lua
    8.4 KB · Views: 305
  • SeedRngr2.zip
    3.1 KB · Views: 278
Last edited:
Sorry for letting this sit here for so long, we weren't really sure what to do to judge this. I tested the included console commands and they seem to work as expected, so I'm just going to go ahead and pass this through.

Welcome to Releases!
 
r2 is now available. This improves the seed extension code and provides more robust checking to ensure networked states aren't modified where they shouldn't be.
 
Last edited:
Status
Not open for further replies.

Who is viewing this thread (Total: 1, Members: 0, Guests: 1)

Back
Top