How Random Number Generators Work
Every computer-generated "random" number is actually pseudo-random. An algorithm takes a seed value and produces a deterministic sequence of numbers that appear random. JavaScript's Math.random() uses a variant of the xorshift128+ algorithm in most modern browsers, producing uniformly distributed 64-bit floating-point values between 0 and 1.
For games, raffles, sampling, and simulations, pseudo-random numbers are perfectly fine. The output passes standard statistical tests for uniformity and independence. Where it falls short: cryptography. If an attacker can determine the internal state of the generator, they can predict every future number. For passwords, tokens, or encryption keys, you need crypto.getRandomValues() instead.
Pseudo-Random vs True Random
| Property | Pseudo-Random (this tool) | True Random (e.g., random.org) |
|---|---|---|
| Source | Mathematical algorithm | Physical phenomenon (atmospheric noise, radioactive decay) |
| Speed | Instant — millions per second | Slower — depends on physical measurement |
| Reproducible | Yes, with same seed | No — inherently unpredictable |
| Good for | Games, sampling, simulations, raffles | Lotteries, cryptographic keys, scientific experiments |
| Crypto-safe | No | Yes |
Common Use Cases for Random Numbers
- Lottery and raffle picks: Generate 6 unique numbers between 1 and 49 for a lottery draw. Disable duplicates so no number repeats.
- Random sampling: Select 100 random participants from a 5,000-person dataset. Use the generated indices to pull specific rows.
- Game mechanics: Simulate dice rolls (1-6), card draws, critical hit chances, or loot drops. Integers only, duplicates allowed.
- A/B test assignment: Generate a random decimal between 0 and 1 for each user. Below 0.5 = variant A, above = variant B.
- Monte Carlo simulations: Generate thousands of random values to model probability distributions, estimate pi, or price financial options.
- Randomized experiments: Assign participants to control vs treatment groups using random number assignment.
Probability Basics: What "Random" Actually Means
A uniform random number generator gives each possible value an equal chance of being selected. For integers between 1 and 10, each number has exactly a 10% chance. This is uniform distribution — the simplest and most common type of randomness.
Key probability concepts that apply to random number generation:
- Independence: Each generated number is independent of the previous ones. Getting a 7 doesn't make 7 less likely next time.
- Law of large numbers: Over many generations, the average of your random numbers converges to the midpoint of the range. Generate 10,000 numbers between 1 and 100 and the average will be close to 50.5.
- Birthday paradox: In a group of 23 people, there's a 50% chance two share a birthday. Similarly, when generating random integers in a range, duplicates appear sooner than intuition suggests.
Tips for Better Random Number Generation
If you need unique numbers, disable duplicates. For integers, you can generate at most max - min + 1 unique values. Requesting more than that returns fewer results than requested.
For decimal numbers, choose decimal places based on your precision needs. Financial simulations typically need 2 decimal places. Scientific modeling might need 6-10. More decimal places means more possible values and virtually zero chance of duplicates.
Use the sum and average statistics to verify your results look reasonable. For a uniform distribution between 1 and 100 with 1,000 numbers, the average should be near 50.5 and the sum near 50,500.
Want to calculate the mean and standard deviation of your generated numbers? Use our average calculator. To find what percentage one value is of another, try the percentage calculator.