CCalclabhub

Work

How To Generate Random Numbers

A random number generator turns a range into a draw. The interesting part is not the numbers themselves but the statistics of the set you get back.

Quick Answer

value = floor(min + u x (max - min)), where u is a uniform draw in [0, 1)

u
A uniform random value between 0 and 1
min
The smallest value the draw may return
max
The largest value the draw may return

A draw of five unique integers from 1 to 100 with the default seed gives 34, 2, 37, 1 and 68, with a median of 34, a mean of 28.4 and a total of 142.

What Is How To Generate Random Numbers?

A random number generator produces values from a range with no pattern that a person could predict. Most software generators are not truly random: they are deterministic algorithms that only look random, and the difference matters when the draw decides something that people care about.

A generator needs a seed, which is a starting number that fixes the entire sequence that follows. The same seed always produces the same sequence, which is exactly what makes a draw reproducible and what makes it unsuitable for a lottery.

A uniform draw means every value in the range has the same chance. Drawing from 1 to 100 gives each integer a one in a hundred chance, and drawing from 0 to 1 gives every real number in between an equal share of the interval.

The core step is scaling. A generator produces a fraction between zero and one, and multiplying that fraction by the width of the range and adding the minimum puts the value in the right interval. The width is the maximum minus the minimum.

Integer draws need one extra step. Dropping the fractional part gives an integer, and the width becomes one value larger than the subtraction suggests, because both endpoints are reachable once the fraction is truncated.

Uniqueness is a filter rather than a different algorithm. The generator keeps drawing and discards anything it has already produced, which works well while the range is much wider than the count and fails badly when they are close.

The birthday problem explains why. Drawing thirty unique values from three hundred and sixty-five is easy, but drawing three hundred from three hundred and sixty-five needs a very large number of attempts.

Decimals are a precision question rather than a probability question. Limiting a draw to three decimal places makes values distinguishable at a grain of a thousandth, which is fine for a simulation and useless for a raffle.

The statistics of a small draw are not the statistics of the range. Five values from 1 to 100 have a mean that lands anywhere near 50 by luck, and the median of an odd-sized draw is simply the middle value once they are sorted.

The mean of a uniform range is its midpoint, and the median of a large draw converges on the same figure. Small draws scatter widely around it, which is why a handful of numbers never looks balanced.

Consecutive draws from a seeded generator are independent in the sense that one value tells you nothing about the next, but they are fixed in the sense that the whole sequence is determined from the start.

A cryptographic generator draws on entropy the operating system collects, such as hardware timing noise. That is the only kind fit for passwords, tokens and raffles, and it cannot be reproduced from a seed.

The calculator draws with a fixed seed by default so that the first screen is identical on every build, and pressing the draw button reseeds it. The range, the count, whether decimals are allowed and whether repeats are permitted are all yours to set.

Formula

value = min + u x (max - min)

The uniform fraction is multiplied by the width of the range and shifted up by the minimum. For integers the fractional part is dropped afterwards.

SymbolMeaning
uUniform draw
minMinimum
maxMaximum
vDrawn value

integer value = floor(min + u x (max - min + 1))

Adding one to the width before truncating makes both endpoints reachable, so an integer draw from 1 to 100 can return either 1 or 100.

SymbolMeaning
minMinimum
maxMaximum
vDrawn integer

expected mean = (min + max) / 2

A uniform range is symmetric, so the midpoint is both its mean and the value a large draw converges on. Small draws scatter widely around it.

SymbolMeaning
minMinimum
maxMaximum
vMean of the range

How To Calculate How To Generate Random Numbers

  1. 1

    Set the range

    Enter the smallest and largest values the draw may return. The width of the range is the maximum minus the minimum, and that width is what the uniform fraction gets scaled by.

  2. 2

    Choose how many values

    Pick the count. Small counts highlight the scatter around the midpoint of the range, while larger counts show the mean settling towards it.

  3. 3

    Decide on decimals and repeats

    Allowing decimals gives continuous values, and forbidding repeats filters out anything already drawn. Forbidding repeats on a narrow range is what makes a draw slow or impossible.

  4. 4

    Draw and read the statistics

    The numbers appear in the order they were drawn, together with the smallest, the largest, the median, the mean, the total and how many distinct values came out.

  5. 5

    Reseed for a fresh draw

    Pressing the draw button advances the seed by one step, which produces a completely different sequence from the same range and count.

Examples

Example 1: Five unique integers from 1 to 100

Minimum
1
Maximum
100
How many
5
Decimals
Not allowed
Repeats
Not allowed
Seed
20261012
StepCalculationResult
Width of the range100 - 199
First drawfloor(1 + u x 99)34
Second drawfloor(1 + u x 99)2
Third drawfloor(1 + u x 99)37
Fourth drawfloor(1 + u x 99)1
Fifth drawfloor(1 + u x 99)68
Mean of the five draws(34 + 2 + 37 + 1 + 68) / 528.4

Result: The five draws come out as 34, 2, 37, 1 and 68, and none of them repeats. Their median is 34, their mean is 28.4 and their total is 142. Five values cover only a twentieth of the hundred possible integers, so a repeat would have been unlikely even without the uniqueness filter.

Example 2: Reseeding the same range and count

Minimum
1
Maximum
200
How many
6
Decimals
Not allowed
Repeats
Not allowed
Seed
20261012 plus one step
StepCalculationResult
Width of the range200 - 1199
The six drawssix unique integers from 1 to 200185, 47, 172, 94, 128, 97
Smallest of the sixmin of the six47
Largest of the sixmax of the six185
Median of the six(97 + 128) / 2112.5
Mean of the six723 / 6120.5

Result: Advancing the seed by one step produces an entirely different set: 185, 47, 172, 94, 128 and 97. The smallest is 47, the largest is 185, the median is 112.5 and the mean is 120.5. Holding the range and the count fixed while changing only the seed is what makes a draw reproducible, and it is also why a seeded generator must never be used for a raffle.

Calculator

Prefer a full-width tool? Open the How To Generate Random Numbers calculator page.

Common Mistakes

  • Using a seeded generator for anything that matters

    A fixed seed reproduces the whole sequence, so anyone who knows the seed knows every value in advance. Passwords, tokens, lottery draws and raffles need an operating system entropy source instead.

  • Forbidding repeats on a narrow range

    Filtering out repeats needs the range to be wider than the count. Asking for fifty unique values from 1 to 20 cannot be satisfied, and a tight case makes the rejection loop very slow before it gives up.

  • Treating the mean of a small draw as the mean of the range

    Five values from 1 to 100 averaged 28.4 on the default seed, a long way below the 50.5 the range is centred on. Small draws scatter, and only large draws converge on the midpoint.

  • Expecting the endpoints to be unreachable

    An integer draw from 1 to 100 does return 1 and 100, because the width is widened by one before the fractional part is dropped. Assuming the upper endpoint is excluded shifts every probability calculation.

  • Reading a pattern into a short sequence

    Runs, gaps and near misses are normal in small samples. A handful of draws that looks uneven tells you nothing about whether the generator is biased.

  • Confusing the seed with the count

    The seed fixes the sequence and the count only takes from it. Changing the count reuses the same prefix of the same sequence, so a smaller draw is always a prefix of the larger one.

  • Rounding before checking distinctness

    Rounding to three decimal places can map two different draws onto the same displayed value, so the distinct count can fall below the requested count even when nothing was rejected.

FAQ

Is a random number generator truly random?

Almost never. A seeded generator is a deterministic algorithm whose output only looks random. Only a generator that reads hardware or operating system entropy is genuinely unpredictable, and it cannot be reproduced from a seed.

What is a seed?

A starting number that fixes the whole sequence. Two generators given the same seed produce identical lists, which is what makes experiments reproducible and what makes the generator useless for anything that must stay secret.

Why does the first screen stay the same on every visit?

Because it is drawn from a fixed seed. That keeps the page cacheable and its content verifiable, and pressing the draw button advances the seed so each interactive draw is different.

Can I draw unique decimals?

Yes, but uniqueness has to be defined on the rounded value. Rounding to three decimal places means two draws closer than a thousandth collide, so the distinct count can be lower than the count you asked for.

How many values can I draw at once?

Up to a thousand. Beyond that the list stops being readable, and the statistics of a very large draw converge on the midpoint of the range anyway.

What is the median of an even number of draws?

The mean of the two middle values once they are sorted. With six draws the median is the average of the third and the fourth, which is why a median of an even-sized draw often lands between two integers.

Should I use this for a raffle?

No. A seeded generator can be reproduced from its seed, and a browser generator is not designed to resist someone who wants to predict it. Use a tool backed by operating system entropy for anything with a prize attached.

References

  1. [1]NIST, Pseudorandom number generators — https://csrc.nist.gov/glossary/term/pseudorandom_number_generator
  2. [2]Python documentation, Random number generation, explained — https://docs.python.org/3/library/random.html
  3. [3]NIST SP 800-90B, Recommendations for randomness — https://csrc.nist.gov/pubs/sp/800/90/b/final