Mersenne Twister (MT19937)
The classic long-period generator behind many language defaults, not for security
Overview
The Mersenne Twister is a pseudorandom number generator with a period of 2^19937 - 1, a 2.5 KB state and good equidistribution. It has been the default in Python, Ruby, PHP, R and C++ standard libraries for decades, but its output is fully predictable after observing enough values.
MT19937 keeps 624 32-bit words of state and generates output with a twisted generalised feedback shift register, followed by a tempering transformation that improves equidistribution. Every 624 outputs it regenerates the whole state. The construction is linear over GF(2), which gives its enormous period and fast generation but also makes it trivially invertible: the tempering is reversible, so 624 consecutive 32-bit outputs reveal the entire state.
Two variants are present: mt19937, the 32-bit generator from 1998, and mt19937-64, the 64-bit version with a 312-word state published in 2000. Modern alternatives such as PCG and xoshiro are smaller, faster and statistically stronger, as MT fails some linearity tests in TestU01's BigCrush. It also recovers slowly from states with many zero bits, a known weakness addressed by later designs such as SFMT and WELL.
History
1998–1998Mersenne Twister published by Makoto Matsumoto, Takuji Nishimura
How it works
- Initialise 624 (or 312 for 64-bit) state words from a seed.
- Regenerate the state block by combining adjacent words with a fixed twist matrix.
- Output each state word after a tempering sequence of shifts, ands and xors.
- Repeat the regeneration after every full block of outputs.
Security notes
Not cryptographically secure: observing 624 consecutive outputs allows complete state recovery and prediction of all further values. Many real vulnerabilities (password reset tokens, session IDs) came from using MT for secrets.
Typical uses
Variants in NextSSL
2| Algorithm | Tier | Sizes (bytes) | Standards | API |
|---|---|---|---|---|
| MT19937 mt19937 |
unverified | out 1–268435456 · seed 4 | MT19937 (Matsumoto, Nishimura 1998), NOT cryptographica… | root |
| MT19937-64 mt19937-64 |
unverified | out 1–268435456 · seed 8 | MT19937-64 (Matsumoto, Nishimura 2000), NOT cryptograph… | root |
Frequently asked questions
Which Mersenne Twister (MT19937) variants does NextSSL include?
NextSSL registers 2 Mersenne Twister (MT19937) variants: mt19937, mt19937-64.