Computers are built to be predictable. Give one the same input and it gives the same output, every time. Yet we ask them for random numbers constantly: to draw raffle tickets, pick survey samples, shuffle quiz questions, create test data and generate encryption keys. How a machine produces "randomness", and how that randomness is squeezed into the range you asked for, decides whether every number really has the same chance.
This guide explains the two families of random number generators, where their randomness comes from, a common mistake called modulo bias that quietly favours some numbers, and the difference between drawing with and without repeats. It finishes with practical ways to generate numbers in a browser, a spreadsheet or code.
Pseudo-random vs cryptographically secure
Pseudo-random number generators (PRNGs)
A PRNG is a formula that turns one number (the seed) into a long sequence that looks random. The same seed always produces the same sequence. That is a feature in simulations and games, where you want to replay a run exactly, and a weakness anywhere fairness or secrecy matters: if someone can learn or guess the seed or observe enough outputs, they may predict what comes next.
JavaScript's Math.random(), the random module in Python and a spreadsheet's RAND() function are all PRNGs. They are fine for shuffling a playlist. They are not designed to resist someone trying to predict them.
Cryptographically secure generators (CSPRNGs)
A CSPRNG is designed so that, even after seeing many outputs, nobody can feasibly predict the next one. Operating systems feed it with hard-to-predict events from the hardware and keep reseeding it. In browsers it is exposed as crypto.getRandomValues(); in Python as the secrets module. This is the kind of randomness used for passwords and keys, and it is also the right choice for any draw where people care about fairness.
"True" randomness
Hardware random generators measure physical noise, such as electrical fluctuations. Modern CSPRNGs already draw on such sources, so for everyday draws, a well-built CSPRNG is as fair as anything you can reasonably use.
The hidden problem: modulo bias
Generators produce raw random bits, for example a byte from 0 to 255. To get a number from 1 to 100, simple code often does this:
number = (randomByte % 100) + 1
The catch is that 256 doesn't divide evenly by 100. Bytes 0 to 99, 100 to 199 and 200 to 255 all map onto the range, but the last block only covers 56 values. So the numbers 1 to 56 can be produced three ways, while 57 to 100 can be produced only two ways. The low numbers are 50% more likely than the high ones.
With larger raw values (32 bits instead of 8), the bias shrinks to something tiny, but it never quite disappears unless the code fixes it. The standard fix is rejection sampling: throw away raw values from the incomplete final block and draw again. Every number in the range then has exactly the same chance.
With repeats or without?
These are two different kinds of draw:
- With replacement (repeats allowed). Each number is drawn independently. Rolling a die ten times is a draw with replacement; getting two sixes is perfectly normal.
- Without replacement (unique numbers). Once a number is drawn, it cannot appear again. Raffles, lotteries and picking five students from a class of thirty work this way.
Without replacement, you cannot draw more unique whole numbers than the range contains: asking for 20 unique numbers between 1 and 10 is impossible. For large draws, the chances change as numbers are removed, which is why lottery odds are calculated with combinations. The chance of matching 6 numbers drawn from 1 to 49 is 1 in 13,983,816, calculated as:
49! / (6! × 43!) = 13,983,816
Generate random numbers in your browser
The Random Number Generator uses the browser's CSPRNG with rejection sampling, so it avoids both predictability and modulo bias.
- Open the Random Number Generator.
- Enter the Minimum and Maximum. Negative values work, for example -50 to 50.
- Set How many numbers, up to 10,000.
- Under Number type, choose Integers or Decimals. For decimals, set Decimal places from 0 to 10.
- Choose a Sort order (Don't sort, Ascending or Descending) and switch on Unique numbers only (no repeats) if needed. If the range is too small for the count, the tool tells you instead of producing duplicates.
- Click Generate. A single number is shown large; several appear as a list with their minimum, maximum and average.
- Use Copy (comma), Copy (lines) or .txt to take the results elsewhere. The screen shows up to 2,000 numbers, but copies and downloads include all of them.
For specialised draws, the Lottery Number Generator, Dice Roller and Coin Flip tools are set up for those formats. To randomise the order of an existing list instead of generating numbers, use the List Randomizer.
Other ways to generate random numbers
In a spreadsheet
Excel, Google Sheets and LibreOffice all have:
=RANDBETWEEN(1, 100) whole number from 1 to 100
=RAND() decimal from 0 up to (not including) 1
Newer versions of Excel also offer RANDARRAY for many values at once. The catch is that spreadsheet random functions recalculate whenever the sheet changes, so your "winner" may change after you press Enter somewhere else. Copy the results and paste them as values straight away. Getting unique numbers requires an extra step, such as generating a random key for each row and sorting.
In code
In Python, use random.randint(1, 100) for simulations and secrets.randbelow(100) + 1 when fairness or security matters. In JavaScript, prefer crypto.getRandomValues() over Math.random() for anything important, and use a rejection loop as described above. On Linux and macOS, shuf -i 1-100 -n 5 prints five unique numbers from the terminal.
Physical methods
Dice, drawn slips and lottery machines have the big advantage of being visible. For a public draw, a physical method can be more convincing to an audience than any screen, even though a good digital generator is at least as fair.
Tips for fair and trustworthy draws
Decide the rules first
Write down the range, the number of winners and whether repeats are allowed before you generate anything. Changing rules after seeing results is where accusations of unfairness start.
Map numbers to people carefully
If entries are numbered from a list, freeze that list before the draw. Removing a duplicate entry after the draw changes which person each number refers to.
Record the moment
For a giveaway, record your screen or have a witness present. Randomness is only half of fairness; the other half is being able to show that you didn't redraw.
Don't expect "evenness"
Random results cluster. Three consecutive numbers, or the same number twice when repeats are allowed, are normal. Why streaks happen is discussed in is a coin flip really fifty-fifty.
Frequently asked questions
Are computer-generated random numbers truly random?
Pseudo-random generators are fully determined by their seed. Cryptographically secure generators, like the one in modern browsers, are unpredictable in practice and suitable for fair draws and security uses.
What is modulo bias?
It is the small unfairness introduced when raw random values are squeezed into a range with the remainder operator and the range doesn't divide the raw range evenly. Rejection sampling removes it.
How do I get random numbers without duplicates?
Turn on "Unique numbers only" in our generator, or in a spreadsheet generate a random key for each value and sort by it. You can't ask for more unique integers than the range contains.
Can I generate random decimals?
Yes. Choose Decimals and set how many decimal places you need, from 0 to 10. Values are spread evenly between your minimum and maximum.
Why do my spreadsheet random numbers keep changing?
RAND and RANDBETWEEN recalculate every time the sheet updates. Copy the cells and paste them as values to freeze the results.