Linear Congruential Generator (State and Parameter Recovery)
- •
What it is
- •
An LCG updates one integer with an affine recurrence. Three consecutive full states usually reveal both unknown parameters when the modulus is known.
- •
- •
When to apply
- •
Use when outputs expose full consecutive states, directly or through reversible digit encoding. Truncated outputs require additional techniques.
- •
- •
Symbols and assumptions
- •
. Known: modulus m and consecutive states . Unknown: multiplier a and increment c.
- •
- •
Math — step by step
- •
- Subtract from . The increment cancels: .
- •
- Let and . If , multiply by its modular inverse: . For prime m, this condition is simply .
- •
- Substitute into the first transition: . Now compute each future state with the recurrence.
- •
- Verify against an unused fourth state. The first three fit by construction, so rechecking only those three is weaker evidence. If d1 is not invertible, solve the linear congruence with its gcd conditions and gather more states to distinguish candidates.
- •
- •
- •
Checks and pitfalls
- •
The pasted Lo-Hi service uses m=2^61−1. Recover complete base-52 states before applying the formula; a missing or extra card corrupts every parameter.
- •
Predicting ranks does not avoid equal-rank losses: the service treats ties as losses regardless of the higher/lower answer. Track funds as well as RNG synchronization.
- •
- •
Related
- •
Python
- •
def recover_lcg(x0, x1, x2, modulus): a = (x2 - x1) * pow(x1 - x0, -1, modulus) % modulus c = (x1 - a*x0) % modulus return a, c a, c = recover_lcg(7, 38, 96, 97) assert (a, c, (a*96+c) % 97) == (5, 3, 95)
- •