How the numbers are generated
This generator uses the Web Crypto API's getRandomValues, which draws from the operating system's cryptographically secure entropy pool rather than a simple arithmetic pseudo-random sequence. That makes the output suitable for draws, raffles, and sampling where predictability would be a problem.
It also applies rejection sampling to avoid modulo bias. Naively taking a random 32-bit integer modulo the range size makes some outcomes slightly more likely than others, because the total number of 32-bit values usually is not an exact multiple of the range. Discarding the small remainder region before taking the modulo restores an exactly uniform distribution.
| Source | How it works | Trust | Typical use |
|---|---|---|---|
| Math.random() | Pseudo-random from a seed | Not for anything where prediction matters | Games, simulations, shuffling a playlist |
| crypto.getRandomValues() (used here) | Cryptographically secure; seeded from OS entropy | Suitable for draws, raffles, tokens | Anything fair or security-sensitive |
| Hardware generator | Samples physical noise directly | Highest assurance | Key generation, gambling machines |
True randomness versus pseudo-randomness
A pseudo-random generator produces a deterministic sequence from a starting seed. Knowing the seed and the algorithm lets you reproduce the entire sequence, which is valuable for reproducible simulations and unacceptable for anything security-sensitive. JavaScript's Math.random is such a generator and offers no guarantees about quality across browsers.
Cryptographically secure generators, like the one used here, are seeded from unpredictable physical sources such as hardware timing jitter and are designed so that observing past output does not reveal future output. Hardware generators go further, sampling physical processes like thermal noise or radioactive decay directly.
Ranges for common uses
Set the range to match what you are simulating. Every value in the range is equally likely, so the chance of any particular result is one divided by the size of the range.
| Use | Range | Chance of a given value |
|---|---|---|
| Coin flip | 0 to 1 | 50% |
| Six-sided die | 1 to 6 | 16.67% |
| Twenty-sided die | 1 to 20 | 5% |
| Percentile | 1 to 100 | 1% |
| Lottery ball, 1 of 49 | 1 to 49 | 2.04% |
| Raffle with 500 tickets | 1 to 500 | 0.2% |
Two dice are not uniform
One die gives every face the same chance; the sum of two does not, because there are more ways to make a 7 than a 2. To simulate two dice, generate two numbers from 1 to 6 and add them rather than one number from 2 to 12 — the second approach gets the probabilities wrong.
| Total | Ways | Probability |
|---|---|---|
| 2 | 1/36 | 2.78% |
| 3 | 2/36 | 5.56% |
| 4 | 3/36 | 8.33% |
| 5 | 4/36 | 11.11% |
| 6 | 5/36 | 13.89% |
| 7 | 6/36 | 16.67% |
| 8 | 5/36 | 13.89% |
| 9 | 4/36 | 11.11% |
| 10 | 3/36 | 8.33% |
| 11 | 2/36 | 5.56% |
| 12 | 1/36 | 2.78% |
Common uses and the no-repeat option
With repeats allowed, each draw is independent — the same number can appear more than once, exactly like rolling a die repeatedly. This is what you want for simulating independent trials.
With no repeats, the result is a random sample without replacement, which is what a raffle or lottery draw needs: once a number is picked, it cannot be picked again. This mode requires the range to be at least as large as the count, since you cannot draw ten unique values from a range of five.