How Do Random Number Generators Work? Pseudorandom vs True Random
A random number generator works in one of two ways. A pseudorandom number generator (PRNG) runs a math formula that starts from a “seed” value and spits out a sequence that looks random but is fully determined by that seed. A true random number generator (TRNG) measures something physically unpredictable, such as electrical noise, radio static or even a wall of lava lamps, and turns it into numbers. Most apps you use, including browser tools, rely on a PRNG that was seeded from real-world noise.
That one paragraph covers most of what people want to know. The rest of this guide explains what a seed actually does, why “pseudo” doesn’t mean “rigged,” where true randomness comes from, and how to tell which kind you need for a raffle, a game or a password.
Why a computer struggles to be random
Computers are built to be predictable. Give a processor the same input and it gives you the same output, every time, which is exactly what you want from a calculator and exactly what you don’t want from a coin toss.
So every RNG has to solve the same puzzle: how do you get unpredictable output from a machine designed to be predictable? There are only two answers. Either you fake it convincingly with math, or you go outside the computer and sample something messy in the physical world.
How a pseudorandom number generator works
A PRNG is a short recipe that takes one number and produces the next. The oldest common recipe is the linear congruential generator (LCG), which follows a formula that fits on a sticky note:
next number = (a × current number + c) mod m
Pick sensible constants for a, c and m, feed in a starting value, and the output jumps around in a way that passes basic statistical checks. Each result becomes the input for the next one.
The seed is the whole story
The starting value is called the seed. Two things follow from it:
- Same seed, same sequence. If you seed a PRNG with 42 today and again next year, you get identical numbers in identical order. Scientists love this because it makes simulations reproducible. Video games use it so a “world seed” rebuilds the same map for every player.
- Different seed, different sequence. Change the seed by one and the output looks completely unrelated.
That is why software normally seeds itself from something that changes constantly, such as the system clock in nanoseconds, mouse movement timing or hardware noise collected by the operating system.
Period: when the pattern finally repeats
Because a PRNG has a fixed amount of internal memory (its “state”), it must eventually loop back to a state it has seen before. The length of that loop is the period. Weak old generators repeated after a few billion numbers. Modern ones are in a different league:
- The Mersenne Twister, developed by Makoto Matsumoto and Takuji Nishimura in 1997 and published in 1998, has a period of 2^19937 − 1. It is the default generator behind Python’s
randommodule. - xorshift128+, which Google’s V8 engine adopted for JavaScript’s
Math.random()in Chrome 49 (late 2015), has a period of 2^128 − 1 and replaced an older generator that failed statistical tests.
For perspective, 2^128 is roughly 340 undecillion. You will not see the loop.
Is a pseudorandom number really random?
Not in the philosophical sense, because it can be recomputed. In the practical sense, a good PRNG is statistically random: every number in your range turns up about equally often, and no visible pattern links one result to the next. Test suites such as TestU01 and the NIST Statistical Test Suite hammer generators with billions of samples to check exactly this.
So for picking a raffle winner, shuffling a playlist or rolling dice in a board game app, a well-built PRNG is fair. Each value gets the same chance, which is all “fair” really means here. You can see this yourself with our random number generator: run a range of 1 to 10 a few hundred times and the counts even out.
Where pseudo becomes a problem: predictability
Fairness and unpredictability are different things. If an attacker learns the internal state of a fast PRNG, they can predict every future output. For a dice game that doesn’t matter. For an encryption key, a session token or a password it is a disaster.
This is why programming documentation is blunt about it. MDN’s page on Math.random() states that it does not provide cryptographically secure random numbers and points developers to crypto.getRandomValues(). Python’s docs give the same warning for random and point to the secrets module instead.
Cryptographically secure generators (CSPRNG)
A cryptographically secure PRNG is still an algorithm, but it is designed so that even someone who has seen a long run of its output cannot work out the next value or the internal state. Operating systems provide one: /dev/urandom and getrandom() on Linux, and similar system APIs on Windows and macOS. Browsers expose it through the Web Crypto API.
The OS keeps it safe by constantly mixing in fresh entropy, unpredictable data collected from things like interrupt timing and hardware noise. Most modern processors also include a built-in hardware random source, for example Intel’s RDRAND instruction, which feeds this pool.
If you need a password or PIN, this is the class of generator you want. Our guide on how to create a strong password explains why length matters more than clever substitutions, and the secure password generator builds one for you in the browser.
How true random number generators work
A TRNG skips the formula and measures nature. Common entropy sources include:
- Thermal noise in resistors and semiconductors, the faint hiss caused by electrons jiggling at any temperature above absolute zero.
- Atmospheric noise. RANDOM.ORG, which Mads Haahr started in 1997 and ran for years from Trinity College Dublin, generates numbers from radios tuned between stations.
- Radioactive decay, where the exact moment an atom decays cannot be predicted.
- Quantum effects, such as which path a single photon takes at a beam splitter.
The lava lamp wall
The most famous example sits in the lobby of Cloudflare’s San Francisco office: a wall of about 100 lava lamps watched by a camera. Cloudflare calls the system LavaRand. Each frame of video, including the shifting wax, changing light, passing visitors and sensor noise, is hashed into random bytes. Those bytes are mixed with local entropy to seed a CSPRNG, and Cloudflare’s production servers pull a fresh chunk of this randomness over TLS when they boot. The company later added a wave machine wall in Lisbon and other “chaos” displays. The idea itself dates back to Silicon Graphics, which built and patented a lava lamp generator in the 1990s.
Notice the pattern: even the lava lamps don’t generate every number directly. They seed a fast algorithm. That hybrid (true randomness in, secure PRNG out) is how nearly every serious system works today, because physical sources are slow and need cleanup to remove bias.
True random vs pseudorandom: a side-by-side
| Pseudorandom (PRNG) | Cryptographic (CSPRNG) | True random (TRNG) | |
|---|---|---|---|
| Source | Math formula + seed | Formula + constant fresh entropy | Physical noise |
| Speed | Very fast | Fast | Slow, rate-limited |
| Reproducible? | Yes, with the seed | No | No |
| Predictable if state leaks? | Yes | No (by design) | No |
| Good for | Games, simulations, shuffles, picking a name | Passwords, keys, tokens | Seeding CSPRNGs, lotteries, research |
Real cases where randomness went wrong
Randomness failures are rarely about a bad formula. They are usually about a bad seed or a rigged process:
- The Hot Lotto fraud. Eddie Tipton, information security director at the Multi-State Lottery Association, programmed lottery number generator software to produce predictable results on certain days of the year. He used it to rig the Hot Lotto draw of 29 December 2010, a $14.3 million prize (the advertised jackpot was $16.5 million). He was first convicted in 2015 and in 2017 admitted to rigging draws in five states. He was sentenced to 25 years in prison.
- Debian’s OpenSSL bug (2008). A Debian-specific code change, first released in September 2006, broke the random number generator in Debian’s OpenSSL package and limited it to just 32,768 possible keys. When Debian announced the flaw in May 2008 (DSA-1571), it told users to regenerate all key material made on affected systems, including SSH, OpenVPN and SSL/TLS keys.
Both stories point to the same lesson. Fairness depends on an honest, unpredictable seed and a process people can verify.
Before computers: a million random digits
In 1955 the RAND Corporation published a book called A Million Random Digits with 100,000 Normal Deviates. The digits came from what RAND called an electronic “roulette wheel”, a random pulse source fed into a counter that produced about one number per second. Researchers used the printed tables for Monte Carlo simulations, sampling and polls for decades. RAND republished it as a paperback in 2001, and its Amazon listing drew hundreds of joke reviews treating it like a novel. It is also a reminder that people needed trustworthy randomness long before apps existed.
Which kind of random do you actually need?
Use this quick test:
- Would anyone gain by predicting the result? If yes (passwords, keys, money), use a CSPRNG.
- Do you need to repeat the exact sequence later? If yes (simulations, game maps, testing), use a seeded PRNG and write the seed down.
- Is it a casual pick? Choosing a winner from 50 comments, a dinner option or a turn order is fine with any decent generator. A spinning name wheel works well when the audience wants to watch the draw happen. Ours picks the winner first with the browser’s secure random source and only then animates, so spin length can’t change the odds. A plain number picker for any range is faster when you just need the digits.
For a giveaway, the generator matters less than transparency. Announce the range and rules before you draw, record the screen, and don’t re-roll a result you dislike. Re-rolling is the one thing that turns a fair generator into an unfair draw.
Common questions about random number generators
Can a random number generator be predicted?
A basic PRNG can be predicted if someone knows its algorithm and current state, or can recover the seed. A cryptographically secure generator is designed so that this is not feasible in practice, even after seeing many outputs.
Is Math.random() truly random?
No. It is a pseudorandom generator (xorshift128+ in Chrome, Edge and Node.js) and is fine for games and casual picks, but browser documentation says not to use it for anything security-related.
Why do random results sometimes repeat?
Because real randomness includes streaks. Getting the same number twice in a row from a range of 1 to 10 happens about 10% of the time. If you need no repeats, use a “unique numbers” setting instead of expecting the generator to avoid duplicates by itself.
What is the most random thing in nature?
Quantum events such as radioactive decay and photon behavior are considered fundamentally unpredictable, which is why they are used in high-end hardware random generators.
