Output-to-seed mappings for CPython's PRNG
TimeLord is a Python demonstration illustrating that a highly improbable sequence from a pseudorandom number generator does not necessarily indicate extraordinary luck. It constructs a seed for Python's random number generator to produce a desired sequence of outcomes. The seed is chosen after the desired outcome is specified, allowing for reproducible results.
Python's standard random module employs the MT19937 Mersenne Twister generator, which has a vast internal state and can generate sequences of arbitrary length. By carefully selecting the seed, TimeLord can control the first 100 outputs of the generator to be heads. This is achieved by constraining the MT19937 output to specific values, which translates to linear equations over the two-element field.
The MT19937 state can be represented as a series of XOR-based linear equations, allowing for the construction of a seed that generates the desired sequence. The process involves reversing CPython's integer-seeding procedure to find the appropriate seed words. Once the seed is generated, it can be used with ordinary Python random-number generation to obtain the same sequence of heads.
The demonstration separates the inversion machinery from the actual text generation, ensuring that the unusual step occurs only when constructing the seed. The demo provides a hexadecimal representation of the seed, and the generated sequence can be printed using the standard random.Random(seed) construct. The important takeaway is that the apparent improbability of the outcome is a result of the intentional seed selection, rather than any inherent randomness.
Written by urgent.news from Lobsters's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.