How Random Number Generators Work (True vs Pseudo-Random)
A random number generator picks values with no predictable pattern. Software usually produces pseudo-random numbers from a formula, while secure generators (such as your browser's crypto source) use unpredictable data from the device. Picking a whole number in a range gives each value a 1 ÷ (max − min + 1) chance.
Pseudo-random numbers
Most programming languages have a generator that follows a formula, starting from a number called a seed. If you know the seed and the formula, you can predict every number. That is fine for games and simulations but unsafe for security.
Secure random numbers
Browsers and operating systems collect unpredictable data (timings, hardware noise) and use it to produce numbers that cannot be guessed. This is what you want for passwords, draws and anything that must be fair.
Picking a number in a range
To pick a whole number from 1 to 10, the generator makes a random number, discards values that would favour some results, and maps the rest to 1–10. Each number has a 10% chance. Naively using "remainder after division" can bias results slightly, so good generators reject the unfair part.
With and without repeats
| Mode | Behaviour | Example |
|---|---|---|
| With repeats | Each pick independent | Rolling a die three times |
| Without repeats | Numbers removed once drawn | Drawing 6 lottery balls from 49 |
Is random really even?
Random does not mean evenly spread. In a short run you will see streaks and repeats, and that is normal. Only over a very large number of picks do the counts even out.
Fairness and limits
- Use unique picks for raffles.
- A random number cannot predict or improve your chances of winning a lottery.
- For official draws, follow the legal rules for your area.
Try the random number generator, roll with the dice roller, or make a secret with the password generator.
Key terms
- Seed: The starting value of a pseudo-random generator.
- Pseudo-random: Produced by a formula.
- Entropy: Unpredictable input used for secure randomness.
- Uniform: Every value equally likely.
- Sampling: Choosing items from a larger set.
Practice questions
Try each one before you read the answer.
- Chance of any number from 1 to 50?
Answer: 2%. - How many unique numbers from 1 to 6 can you pick at most?
Answer: 6. - Pick 6 unique numbers from 1 to 49: how many ways?
Answer: 13,983,816 combinations.
Examples of good and poor uses
- Good: choosing raffle winners, picking a random order for presentations, splitting a class into teams, selecting rows to sample for a test.
- Poor: creating encryption keys or passwords with a simple generator that is not secure. Use a secure source or the password generator.
The birthday effect
With only 23 people, there is already a better than 50% chance that two share a birthday. Random picks repeat much sooner than most people expect, which is why a generator that never repeats a number can actually look less random than a real one.
Shuffling a list
To put a list in random order, pick a random item for each position without repeats. A fair method is the Fisher-Yates shuffle, which swaps each item with a randomly chosen earlier item. A biased method, such as sorting by a random value with a poor comparison, can favour some orders.
How many numbers do you need?
For a fair draw among 200 entries you only need numbers from 1 to 200. For sampling, pick unique row numbers so that no row is chosen twice.
Frequently asked questions
Is Math.random secure?
No. It is fine for games, but secure uses should rely on the crypto random source.
Can a random number be predicted?
A pseudo-random one can if you know the seed. A secure one cannot.
What is a seed?
The starting value of a pseudo-random generator. The same seed gives the same sequence.