4 ms·
What kind of wizardry is this doing?! :)
by aprdm 5y ago
What kind of wizardry is this doing?! :)
- llimllib 5y agoSeems that's one of these: https://en.wikipedia.org/wiki/Lehmer_random_number_generator https://en.wikipedia.org/wiki/Lehmer_random_number_generator
- Dylan16807 5y agoIt's one of the simplest ways to make random numbers. Multiply state by a constant, return the upper bits/digits of state. There's some math that goes into picking a good constant, but honestly that's optional. Usually you'd also add a constant each round but this is going for absolute simplicity. And the line with 'static' is just an awkward way of initializing the state integer.
- tirpen 5y agoThose numbers are not random.
- Someone 5y ago> There's some math that goes into picking a good constant, but honestly that's optional. It isn’t optional. For example, if your constant is even, every iteration adds another zero to the end of your state, and after (in this case) 127 iterations, you’ll start returning zeroes forever. Also, if your multiplier is smallish, the lower order ones of the current value can be used to (somewhat) predict the higher order ones of the next value.
- Dylan16807 5y agoYes it has to be odd and it has to be the size asked for. I don't count checking that as math. If you're particularly worried about the top couple digits of the number being undersized you could return fewer bits or use a wider multiplier.
- loeg 5y agoIt’s a multiplicative congruential generator (MCG), a subset of linear congruential generators (LCG). Basically a multipy (and for LCG, an add) between each generated number. Very fast, small state requirements. Wholly unsuitable for cryptographic random numbers.