Course contentsShow
← Security
Lesson 287 of 3,0527. Randomness and Key ManagementFree lesson

PRNGs: Linear Congruential and Mersenne Twister

Study common PRNG algorithms and why they are unsuitable for cryptographic purposes despite good statistical properties.

PRNGs: Linear Congruential and Mersenne Twister

What you'll learn: Why two popular pseudo-random number generators work well for simulations but fail catastrophically for cryptography.


Now that you understand the difference between true and pseudo randomness, let's examine two common PRNGs that are statistically excellent but cryptographically terrible.

Linear Congruential Generator (LCG)

The LCG uses a simple formula: next = (a × current + c) mod m, where a, c, and m are carefully chosen constants. Given a seed, it produces a predictable sequence of numbers that appear random.

The problem: If an attacker sees even a few outputs, they can reverse-engineer the internal state and predict all future values. It's like a magician doing card tricks—impressive at first glance, but once you know the method, it's completely predictable.

Mersenne Twister

The Mersenne Twister (MT19937) is far more sophisticated, using 624 previous values and complex bit-shifting operations. It has excellent statistical properties and a massive period (2^19937 - 1). Many programming languages use it as their default random number generator.

The problem: With just 624 consecutive outputs, an attacker can clone the entire internal state. It's also not designed to be unpredictable—the algorithm assumes you can't see the outputs, which is false in cryptographic contexts.

Why This Matters

Both algorithms excel at Monte Carlo simulations, games, and testing—anywhere you need fast, statistically distributed numbers. But for generating encryption keys, session tokens, or passwords? They're dangerously predictable.

Think of them like a complex clock: perfect for measuring time intervals, useless for securing your house.

Key Takeaway: Linear Congruential Generators and Mersenne Twister produce statistically good randomness very efficiently, but their predictability makes them completely unsuitable for any cryptographic purpose—always use cryptographic PRNGs (CSPRNGs) for security applications.