On this page
Call random.random() in Python and you are using the Mersenne Twister. Python's documentation names it the core generator, gives its period as 2**19937-1, and warns that it is "completely unsuitable for cryptographic purposes" (Python random module documentation).
What is the Mersenne Twister?
The Mersenne Twister is a pseudo-random number generator: a fixed rule that turns a seed into a long stream of numbers that pass statistical tests for randomness. Makoto Matsumoto and Takuji Nishimura published it in 1998 (Matsumoto and Nishimura, 1998).
Their abstract states that MT19937 has a period of 2^19937 − 1, is 623-dimensionally equidistributed up to 32-bit accuracy, and uses a working area of only 624 words. Like a linear congruential generator, it is fully determined by its seed.
Why is the period 2^19937 − 1?
The state is 624 words of 32 bits, but one word contributes only its top bit: the authors' reference C code joins the upper bit (mask 0x80000000) of word i to the lower 31 bits of word i+1, then overwrites word i (mt19937ar.c, Hiroshima University). So the state that matters is 623 × 32 + 1 = 19,937 bits.
Every step is built from shifts and XORs, so it is linear over bits. The all-zero state maps to itself and can never join the cycle, leaving at most 2^19937 − 1 states; the chosen parameters make the generator visit all of them before repeating.
Why must 2^19937 − 1 be prime? The full period is reached exactly when the characteristic polynomial is primitive, and when 2^p − 1 is prime every irreducible polynomial of degree p is primitive, because the order of x must divide a prime. The paper calls MT a TGFSR variant "modified so as to admit a Mersenne-prime period" (Matsumoto and Nishimura, 1998).
2^19937 − 1 has 6,002 digits and was shown prime by Tuckerman in 1971 (Prime Pages table of known Mersenne primes); primes of this shape are proved with the Lucas–Lehmer test for Mersenne primes.
How do the twist and temper steps work?
In outline, the reference code does two jobs (mt19937ar.c):
Twist. For each position i, join the top bit of word i to the lower 31 bits of word i+1 to make y. Shift y right by one, XOR in 0x9908b0df if y was odd, then XOR with word i+397 (positions wrap round at 624).
Temper. Each word goes through four shift-and-XOR steps before it is output: y ^= y >> 11, then y ^= (y << 7) & 0x9d2c5680, then y ^= (y << 15) & 0xefc60000, then y ^= y >> 18.
Tempering is an invertible matrix applied on the way out, so it mixes bits within each word without changing the period (Mersenne Twister, Wikipedia).
Worked example: temper y = 0x00000001.
y >> 11 is 0, so y stays 0x00000001.
y << 7 is 0x00000080, and 0x80 AND 0x9d2c5680 is 0x80, so y becomes 0x00000081.
y << 15 is 0x00408000; AND 0xefc60000 keeps only 0x00400000, so y becomes 0x00400081.
y >> 18 is 0x00000010, so y becomes 0x00400091.
One input bit has become four output bits (decimal 4,194,449). If bitwise operations are new, the Fermi Concept Library gives every concept in mathematics and computing its own lesson.
What does 623-dimensional equidistribution mean?
Take runs of k consecutive 32-bit outputs. Being k-distributed to 32-bit accuracy means that over a full period, every possible run of k values turns up equally often (the all-zero run once fewer). MT19937 has this for every k from 1 to 623 (Matsumoto and Nishimura, 1998).
Why stop at 623? A run of 624 words would need 624 × 32 = 19,968 bits of freedom, but the state holds 19,937, so 623 is the ceiling. This is a guarantee about the whole cycle, not proof that every short stretch looks random. When even spacing matters more than randomness, see quasi-Monte Carlo with the van der Corput sequence.
Why is the Mersenne Twister not safe for cryptography?
The authors' own FAQ says MT "is not cryptographically secure" because it "is based on a linear recursion", and the rest of such a sequence can be predicted from a long enough run of outputs (Mersenne Twister FAQ, Hiroshima University).
For MT19937 that run is 624 outputs, the size of the state vector (Mersenne Twister, Wikipedia). Each output is one tempered state word, and tempering can be undone: in step 4 above, the top 18 bits pass through unchanged and reveal the rest.
Which random generator should you use?
Dice simulations, Monte Carlo estimates of pi, shuffling practice questions: the Mersenne Twister is fine. Set a seed so you can rerun the experiment.
One-time passwords, reset links, session tokens, prize draws: do not use it. Python's secrets module exists for this, since random "is designed for modelling and simulation, not security or cryptography" (Python secrets documentation).
Learning the algorithm: code the twist and temper, then an untemper, and confirm it returns the word you started with.
Fermi's AI tutor can work out what you already know and build a plan for the session, so the bit operations come before the polynomials.



