Search code examples
pythonrandomlcg

Code for reversing Linear Congruential Generator in python


index 0 of x is the seed, but what if, you want to know the index -1 of x.

I am making a game in python with pygame, and i require help, since i require to use that in world generation

Edit:

x is the random number, index 0 of x is seed

the LCG looks like this

x[i + 1] = (x[i] * mu + inc) % m

m is modulus

inc is increment

mu is multiplier


Solution

  • If xi+1 = xi * mu + inc (mod m), then xi = (xi+1 - inc) * mu−1 (mod m), where mu−1 is the modular multiplicative inverse of mu (mod m). It's a fairly straightforward calculation.