[Open Assets] Pseudorandom numbers library

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

LJ Sonik

Developer
Sonic Team Junior
WARNING, THIS SCRIPT DOES NOT WORK.



A little script that features functions returning pseudorandom numbers which are usable with netgames, including dedicated servers. I used the Xorshift algorithm, which is a simple, fast and reliable enough method for a game like srb2.
To assure synchronization on script loading, the numbers generated in the first map played will always follow the same suit. This won't happend once the map has been changed thought.

To use it in your mod, just insert it in a separate lump then you can use the following functions in the other scripts:

  • A_Random(): returns a random value from -2147483648 and 2147483647.
  • A_RandomKey(INT32 n): returns a random value from 0 to (n-1).
  • A_RandomRange(INT32 a, INT32 b): returns a random value from a to b, with a < b.
  • A_RandomDatas(): returns the mobj used to store the server-synchronized variables. Usually needed to check if the previous functions are already functional

Warning ! As the library uses a mobj to synchronize datas, you cannot use random numbers on the few first tics after map changing ! You have to check if the library is initialized with a test like "if not A_RandomDatas() return end". Tip: Don't use this check everytime you try getting a random number, make a global check if possible.

To dump the stupid bug with *.lua files, I add a screenshot of a mod using that library:

srb20019.png

Ho, and another one, because the site won't let me post this thread else:

srb20073.png
 

Attachments

  • random.lua
    926 bytes · Views: 474
Last edited:
This works and I'm happy to pass it. It's a pretty simple script and it's also competing with the Linedef type 451 (Random Range) but I imagine someone will find a use for this somewhere.

Also it's worth noting that you can find an example of this script in action here.
 
Ho I already used it many times. It is very useful every time you create a script that is meant to work with netgames and that would use P_Random(), P_RandomKey(), RandomRange(), or similar functions from the default Lua library.
 
Status
Not open for further replies.

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

Back
Top