Hey there! I've been working on an arcade game in SP, and I've come across the need for a random number generator. I figured that I'd build a function for this, so here we are. After some research, I decided to try and implement the linear congruential generator algorithm, which is simple and effective enough for really any use a SP builder may have.
Here it is in action:
And the code--done entirely in variables:
a, c, and m are all parameters involved with the LCG itself. These parameters are just some reference parameters I grabbed from Wikipedia supposedly used on a computer called the ZX81, but they seem to work well enough.
The m parameter inherently limits the max possible value from the raw RNG function, in this case 2^16 - 1. The actual label is slightly processed to be limited between 0 and 100 (by dividing by m and then multiplying 100 as to not hurt the randomness of the number).
The first line of
setSeed have to do with setting a seed for the RNG. It is based on the game timer, and it is fixed once you click on the RNG button (or any other "event" to trigger this). Thus, you can pretty much guarantee that you will get effectively completely random numbers from this generator.
This has a lot of uses, so I'm pretty excited! Feel free to play with and modify the RNG generator here.
See you in my next upload!