On this concept: Formula

Not sure which lesson to do first? Find your level and get a plan →

CS Thinking · Computational Thinking · Grade 6-8

Random Numbers

Random numbers are values with no predictable pattern.

P(r=i)=1n

Quick Answer

Random numbers are values with no predictable pattern. Programs use them wherever chance matters: rolling a die in a game, shuffling cards, picking a random sample, adding chance events to a simulation. Most computers make pseudo-random numbers: a formula starts from a seed and produces numbers that look random but repeat exactly if you start from the same seed. A fair generator gives each of n allowed values the same chance, 1n.

Why This Matters

Random numbers make games unpredictable, let simulations try thousands of possible futures, and let scientists choose fair samples. The seed matters both ways: reusing it lets you replay a bug or share a game level exactly, but a guessable seed lets someone predict the 'random' numbers, which is why passwords and encryption need special generators. A careless rule that makes one value more likely quietly biases every result.

Intuitive Explanation

A fair die has six faces, so each roll gives 1 to 6, each with probability 16, and no roll tells you anything about the next. A computer cannot roll a die, so it fakes one with a formula. A tiny example: start from a seed, say 7. At each step, multiply by 5, add 3, and keep the remainder after dividing by 16. From 7: 7 × 5 + 3 = 38, remainder 6. Then 6 × 5 + 3 = 33, remainder 1. Then 8, then 11, then 10. The numbers jump around with no obvious pattern, yet they are completely fixed by the seed: start at 7 again and you get 6, 1, 8, 11, 10 again.

That is a pseudo-random number generator. Real ones use far bigger numbers, so the sequence runs for billions of steps before repeating and passes statistical tests for randomness. To turn its output into a die roll, a program maps it onto 1 to 6, and the mapping has to keep every face equally likely; a careless one can favor some faces.

Random does not mean evenly spread in the short run. Roll a fair die 12 times and getting no 6 at all happens about 11% of the time, and a run of high numbers does not make a low one 'due', because each roll is independent. Random numbers are also not the same as a simulation. A random number is one ingredient, such as whether one customer arrives this minute; the simulation is the whole model that uses thousands of them.

Think of it like this. A pseudo-random generator is like a very long shuffled playlist: the songs seem to come in no order, but the order was fixed the moment the shuffle was made. Start the same playlist from the same point, the seed, and you hear exactly the same sequence. A truly random source, such as radioactive decay, has no playlist at all.

Core idea. Random numbers help models include uncertainty, variation, and chance.


When to Use

Use random numbers when a program needs a value decided by chance: a die roll, a shuffle, a random enemy position, a random sample, or the chance events inside a simulation. Decide the range of values and whether each must be equally likely. Use a fixed seed when a run must be repeatable. If the value can be worked out from the inputs, compute it instead. If the question is about the whole model that uses the chance events, that is Simulation.

Tip. Ask: Does the program need a value picked by chance, such as a die roll or a shuffle, instead of one calculated from its inputs?

How to Recognize It

Check whether something in the task must be left to chance, and how fair or repeatable that chance has to be.

  1. Should the result be unpredictable to the user, like where the next enemy appears?

    Yes needs random numbers. If the same inputs should always give the same output, as in a grade calculator, randomness does not belong there.

  2. Is the task to make one chance value, or a few, rather than to run a whole system over time?

    Yes keeps you at random numbers. If the question is about a disease or traffic model that uses those values step after step, it is Simulation.

  3. Must every allowed value be equally likely, each with chance 1n?

    Yes is a uniform random number. If the question asks how likely an event is, with no number being generated, it is Basic Probability.

  4. Is the question about how the computer produces chance values, not about who gets picked for a survey?

    Yes is random numbers. If it is about choosing survey members so each has an equal chance, it is Random Sampling, which uses random numbers as a tool.

Random Numbers vs Simulation vs Random Sampling vs Basic Probability

All four involve chance, and a single project can use every one. Ask whether you are producing chance values, running a model with them, choosing a fair sample, or calculating how likely something is.

Random Numbers

Meaning
Random numbers are chance values that a program generates, usually with a seeded formula that only imitates true randomness.
Key test
A value is being produced by chance inside a program.
Formula
P(r=i)=1n
Example
random.randint(1, 6) in Python returns a whole number from 1 to 6 for a die roll.

Simulation

Meaning
A simulation runs a model of a real system over many steps, often drawing random numbers along the way.
Key test
A whole system changes over time; chance values are only one ingredient.
Formula
St+1=f(St,P)
Example
Playing 10,000 computer games of Monopoly to see which squares players land on most often.

Random Sampling

Meaning
Random sampling chooses members of a group so that every member has the same chance of being picked.
Key test
The goal is a fair sample of people or items for a study.
Example
Drawing 30 student ID numbers at random from 600 to survey about lunch.

Basic Probability

Meaning
Probability is a number from 0 to 1 that measures how likely an event is.
Key test
You calculate a chance; nothing is being generated or picked.
Formula
P(event)=favorable outcomestotal outcomes
Example
The chance of rolling a 5 on one fair die is 16.

Formula & Notation

P(r=i)=1n
A random number generator produces values intended to approximate a target probability distribution. In many programs, the generator is pseudo-random and controlled by an initial seed value.

Worked Examples

Example 1 — Chance of a win, and how many to expect

Easy

A game picks a random whole number from 1 to 20 each round, all equally likely, and the player wins on 18, 19 or 20. What is the chance of a win in one round? About how many wins should 200 rounds give?
Show the steps and the answer
  1. Count the values: 1 to 20 is 20 equally likely numbers, so each has probability 120.

    A fair generator gives each of n allowed values the same chance, 1n.

  2. Count the winning values: 18, 19 and 20, which is 3. So P(win) = 320=0.15.

    The chances of separate values add up.

  3. Expected wins in 200 rounds: 200 × 0.15 = 30.

    Over many rounds, the share of wins settles near the probability.

  4. Allow for variation: a fair run might give 25 or 36 wins, but 60 wins would point to a bug or a biased generator.

    Random results scatter around the expected value, but not by that much.

Answer: The chance of a win is 320=0.15, so about 30 wins in 200 rounds.

Takeaway: With n equally likely values, each has chance 1n; multiply by the number of rounds to predict how often an outcome should appear.

Example 2 — Same seed, same 'random' numbers

Standard

A game places treasure chests using the generator next = (5 × previous + 3) mod 16, giving positions 0 to 15. Ana and Ben both start it with seed 3; Cara starts with seed 10. List the first 3 positions for each player. Are these numbers random?
Show the steps and the answer
  1. Seed 3: 5 × 3 + 3 = 18, remainder 2; 5 × 2 + 3 = 13; 5 × 13 + 3 = 68, remainder 4. Positions 2, 13, 4.

    Each output becomes the next input, so the whole sequence follows from the seed.

  2. Ben also uses seed 3, so he gets exactly 2, 13, 4.

    Same seed and same formula give the same sequence, every time.

  3. Seed 10: 5 × 10 + 3 = 53, remainder 5; 5 × 5 + 3 = 28, remainder 12; 5 × 12 + 3 = 63, remainder 15. Positions 5, 12, 15.

    A different seed starts the sequence at a different point.

  4. Decide: the positions look patternless to a player but are fully predictable from the seed, so they are pseudo-random.

    That predictability is useful for sharing a level or replaying a bug, and dangerous for anything secret, such as passwords.

Answer: Ana and Ben: 2, 13, 4. Cara: 5, 12, 15. The numbers are pseudo-random: unpredictable to a player, but fixed by the seed.

Takeaway: A pseudo-random sequence is decided by its seed: same seed, same numbers. That helps with replays and hurts with security.

Example 3 — Find the mistake: a die that favors some faces

Application

A generator gives whole numbers from 0 to 15, each equally likely. Jun turns them into die rolls with roll = (r mod 6) + 1 and writes: "Every r gives a roll from 1 to 6, so all six faces are equally likely." Find the mistake and correct it.
Show the steps and the answer
  1. List which values of r give each face: 0, 6, 12 → 1; 1, 7, 13 → 2; 2, 8, 14 → 3; 3, 9, 15 → 4; 4, 10 → 5; 5, 11 → 6.

    Listing every input shows exactly how the 16 values are shared among the 6 faces.

  2. Work out the chances: faces 1 to 4 each get 316, faces 5 and 6 only 216.

    16 values cannot be split evenly into 6 groups, so some faces get an extra value.

  3. See the effect: in 1,600 rolls, expect about 1600×316=300 of each face from 1 to 4 but only 1600×216=200 of a 5 or a 6.

    A gap of 100 between faces is a built-in bias, not bad luck.

  4. Fix it: if r is 12 or more, throw it away and draw again; use (r mod 6) + 1 only for r = 0 to 11.

    The 12 remaining values split evenly, 2 per face, so each face has chance 212=16.

Answer: Reject r = 12 to 15 and draw again. Each face then has probability 16, instead of 316 for faces 1 to 4 and 216 for faces 5 and 6.

Takeaway: Every value being possible is not the same as every value being equally likely; count how many inputs lead to each output.

Where You'll See It

  • Shuffling a deck in a card game app

    The app uses a random-number generator to pick the order of cards so each deal feels unpredictable — but it is really a deterministic algorithm producing numbers mixed enough to seem random.

  • Replaying the same game level with a seed

    Speedrunners can share a 'seed' so everyone gets the exact same 'random' enemy placements — because the same seed feeds the generator the same starting point, it recreates the identical sequence.

  • Rolling a fair die in code

    To simulate a 6-sided die you want each face to have probability 1/6 — a uniform random generator gives P(roll = i) = 1/6, so no number is secretly favored.

Common Mistakes

  1. The slip: Computer random numbers are truly random and impossible to predict.

    Why it's tempting: They look unpredictable to us, so they seem genuinely random.

    Instead: Most are PSEUDO-random: produced by a deterministic formula from a seed. Knowing the algorithm and seed lets you reproduce the exact sequence — which is why they are unsafe for serious cryptography without special generators.

  2. The slip: After getting several high numbers, a low number is 'due' to balance it out.

    Why it's tempting: Our intuition expects fairness to correct itself quickly (the gambler's fallacy).

    Instead: Each draw from a uniform generator is independent — past results do not change the next probability. Every value still has probability 1/n on the next draw, no matter the recent streak.

  3. The slip: Turning a generator value from 0 to 15 into a die roll with (value % 6) + 1 gives a fair die.

    Why it's tempting: Every face from 1 to 6 can come out, so it looks as if each face gets an equal turn.

    Instead: 16 values do not split evenly into 6 faces: faces 1 to 4 each come from 3 of the 16 values and faces 5 and 6 from only 2, so P(1) = 316 but P(6) = 216. Count how many raw values land on each outcome; rerolling any value from 12 to 15 leaves 12 values, 2 per face.


Mini Practice

Try these on your own. Tap Reveal when you want to check.

  1. A program picks a whole number from 1 to 8, each equally likely. What is the probability of picking 3? Of picking an even number?

    Hint: Use P=1n for one value. Then count the even numbers.

  2. random() gives a decimal from 0 up to, but not including, 1. The code is roll = floor(random() * 6) + 1. What values can roll take? What is roll when random() gives 0.73?

    Hint: Find the smallest and largest possible values of floor(random() * 6) first.

  3. A simulated die is rolled 600 times. About how many 4s should appear? If 4 came up 300 times, what would you suspect?

    Hint: Each face should get about 16 of the rolls.

  4. A generator uses next = (5 × current + 3) mod 16, where mod means the remainder after dividing by 16. Starting with seed 7, give the next three numbers.

    Hint: 5 × 7 + 3 = 38. Find the remainder when 38 is divided by 16.

  5. Task A: produce one fair coin flip. Task B: flip 1,000 virtual coins each day for 30 days to model how a rumour spreads in a school. Which is just random numbers, and which is a simulation?

    Hint: Which task models a system changing step by step over time?

  6. A tester runs a dice game twice with the same seed and gets exactly the same rolls. They say: 'The random function is broken.' Are they right?

    Hint: How does a pseudo-random generator make its numbers?

Want the full set?

50 practice questions for this concept — free to try, every one with a complete worked solution showing the why, not just the answer.

Frequently Asked Questions

Where does the seed come from if I do not choose one?

Most languages use something that changes all the time, such as the current time in tiny fractions of a second, or noise the computer collects from things like mouse movements and network timing. Because you never start a program at exactly the same instant, the seed differs every run and so does the sequence. Choosing the seed yourself, as in random.seed(42), makes every run repeat.

Why does a music shuffle sometimes play two songs by the same artist in a row?

Because true randomness often produces runs that look planned. With 10 artists, the chance that the next song shares the current artist is about 1 in 10, so in a long session it happens many times. Listeners complained that this felt broken, so Spotify changed its shuffle to spread out songs by the same artist, making it less random on purpose so that it feels more random.

How do I get a whole number from 1 to 6 out of a generator that gives decimals from 0 to 1?

Multiply by 6, round down, and add 1. A value like 0.73 becomes 0.73×6=4.380.73 \times 6 = 4.38, rounds down to 4, and becomes 5. Every decimal from 0 up to just under 1 lands in one of six equal slices, so each face has chance 16\frac{1}{6}. Forgetting to add 1 gives the numbers 0 to 5 instead, a classic bug.

Learning Path

← Before

Simulation
Random Numbers

You are here

Next →

Modeling
You need Simulation first: a state and rules that imitate a real system step by step. Random Numbers add chance to those rules, through pseudo-random generators, seeds and equally likely values. Next, Modeling uses them to build and check models of uncertain systems, such as how many customers arrive at a shop each hour.

See Also