PickRandom Logo

PickRandom

Technology

How Computers Generate Random Numbers: PRNG vs CSPRNG Explained

A clear explanation of how computers generate random numbers — covering pseudo-random number generators (PRNG), cryptographically secure RNGs (CSPRNG), and why the difference matters.

Quick Answer: Computers generate random numbers using mathematical algorithms. Standard algorithms (PRNG) produce sequences that appear random but are mathematically predictable. Cryptographic algorithms (CSPRNG) collect real-world entropy (system noise, hardware events) making their output computationally impossible to predict.

Why Computer Randomness Is Different From Physical Randomness

A computer is a deterministic machine — given the same input, it produces the same output every time. True randomness requires unpredictability, which is fundamentally at odds with determinism. Computers achieve "randomness" either by using mathematical algorithms that produce unpredictable-looking sequences, or by measuring genuinely unpredictable physical phenomena.

Pseudo-Random Number Generators (PRNG)

A PRNG uses a mathematical formula seeded with an initial value (the "seed") to produce a sequence of numbers. The same seed always produces the same sequence. JavaScript's Math.random() is a PRNG — fast, sufficient for games and simulations, but predictable if the seed is known. Popular PRNG algorithms include Mersenne Twister and Xorshift.

Cryptographically Secure PRNGs (CSPRNG)

A CSPRNG adds an extra requirement: even if you know many output values, you cannot predict future values or determine the seed. This is achieved by:

  • Collecting entropy from unpredictable hardware events (keystrokes, mouse movements, network timing, CPU temperature fluctuations)
  • Mixing this entropy into the generator's state continuously
  • Using one-way mathematical functions that cannot be reversed to reveal the internal state

The Web Crypto API

Modern browsers implement the Web Crypto API — specifically window.crypto.getRandomValues() — as their CSPRNG interface. This is the same API used for generating TLS/SSL encryption keys. PickRandom.online uses this API exclusively, giving every coin flip, dice roll, and number generation bank-grade cryptographic randomness.

TypeExamplePredictable?Use Case
PRNGMath.random()Yes, if seed knownGames, simulations, non-security use
CSPRNGWeb Crypto APINo — computationally impossibleEncryption, security, fair random tools
Hardware RNGAtmospheric noiseNo — physically randomSeed sources for CSPRNGs

Frequently Asked Questions

What is the difference between PRNG and CSPRNG?

A PRNG uses a deterministic algorithm to generate sequences that appear random but are predictable if the seed is known. A CSPRNG also uses an algorithm but collects real-world entropy and uses one-way functions, making it computationally impossible to predict future values.

Is Math.random() good enough for random number generation?

For games, simulations, and non-security purposes, yes. For anything requiring genuine fairness or security (lotteries, cryptography, privacy tools), use a CSPRNG like the Web Crypto API.

How does PickRandom.online generate random numbers?

Using window.crypto.getRandomValues() — the Web Crypto API CSPRNG built into every modern browser. This is the same standard used for generating HTTPS encryption keys.