GCSE · Computer Science · AQA · Spec 8525

Random number generation

Imagine a dice game where every roll comes up the same: six, six, six, six. The code asks for a random number. It just asks once, in the wrong place.

Computer Science · Watch the variables

One call, one roll

A tiny dice game: it rolls three times and counts the sixes. Step through it and keep your eye on roll in the Variables panel. These rolls are one possible run. Run it again and you could get completely different numbers.

Before you press Next: roll gets set three times. What's the smallest value it could ever hold, and the biggest?

1 sixes ← 0
2 FOR turn ← 1 TO 3
3 roll ← RANDOM_INT(1, 6)
4 OUTPUT roll
5 IF roll = 6 THEN
6 sixes ← sixes + 1
7 ENDIF
8 ENDFOR
9 OUTPUT "Sixes: ", sixes

Variables

sixes=0

Output

 

Step 1: sixes starts at 0. It will count how many sixes get rolled.

1 / 15

Press Next and watch roll. It only changes on line 3, the line with the random call.

Step 1 of 15: sixes starts at 0. It will count how many sixes get rolled..

Exam line: To use a random number: call the function with the range you need, store the result in a variable, then use that variable.
Watch out: roll doesn't change every time you use it. Only a new call to RANDOM_INT gives it a new random value.

Computer Science · What can it give?

Which numbers can come out?

A quiz program picks the number of its next question with the line q ← RANDOM_INT(1, 10).

Which values could q end up holding? Pick the one closest to what you think right now.
How sure are you?

Computer Science · Find the bug

Heads, heads, heads, heads, heads

This program should toss a coin five times and show Heads or Tails for each toss. Instead, every run shows the same result five times in a row. Which line is causing it?

A program with one bug — which line goes wrong?

Computer Science · Your turn

Write it: two dice

Use RANDOM_INT(low, high), which gives a random whole number from low to high, both included. Pseudo-code or any programming language you know is fine.

Write a program that simulates rolling two ordinary six-sided dice. It should store each roll in its own variable, then output the total of the two rolls. [4 marks]

0 words · your answer stays on this page and is not sent anywhere.

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

Roll a dice in code, keep the result, and use it, with a fresh roll every time you need one.

What you need to know

  • How to read a random-number call and list every value it can give
  • Why a random number is stored in a variable before the program uses it
  • Why the variable keeps the same value until the function is called again
  • Where to put the call so you get a fresh value each time you need one
  • How to write a short program that generates, stores and uses random numbers

The big picture

In this lesson's pseudo-code, RANDOM_INT(1, 6) gives a random whole number from 1 to 6, and both 1 and 6 can come up. You store the result in a variable, like roll ← RANDOM_INT(1, 6), so later lines can check it, add it up or output it. The variable keeps that value until something new is stored in it. For a fresh random number, call the function again: inside a loop, that gives a new value on every pass; before the loop, just one for the whole run.

Key points

1A random-number function gives your program a value it can't predict. RANDOM_INT(1, 6) gives a whole number from 1 to 6, and both 1 and 6 can come up.
2Languages write this differently, and some leave the top number out. Always check which values your function can give.
3Store the result in a variable: roll ← RANDOM_INT(1, 6). Then later lines can check it, do maths with it and output it.
4A variable doesn't re-roll itself. It keeps its value until a new value is stored in it.
5Each call makes a new random value, which could still match the last one by chance. Want a new value on every pass of a loop? Put the call inside the loop.
6Need two separate random values, like two dice? Make two calls. Copying one variable into another just copies the same number.

Worked example

Problem

Write pseudo-code for this game rule. The player starts with 100 points and gets a random bonus of 10 to 50 points (both included). Add the bonus to the score and output the new score. If the bonus was more than 40, also output "Big bonus!".

⚠ Watch out

Calling the random function once, before a loop, and expecting a new value each time round. The variable just keeps that first value, so every pass uses the same number. Move the call inside the loop.

🧠

Memory hook

One call, one roll. A variable remembers its roll. It never rolls again by itself.

✓

Check yourself

x ← RANDOM_INT(5, 8) runs once, then x is output three times. Which values could x hold? Could the three outputs differ? What change would give three fresh values?

Flashcards

(6)
What can RANDOM_INT(1, 6) give?
Any whole number from 1 to 6. Both 1 and 6 can come up.
Why store a random number in a variable?
So later lines can use that same value: check it, add it up or output it.
Does a variable holding a random number change each time you use it?
No. It keeps its value until something new is stored in it, such as the result of another call.
You want a fresh random value on every pass of a loop. Where does the call go?
Inside the loop. A call before the loop runs once, so every pass reuses that one value.
Does a new call always give a different number from the last one?
No. Each call gives a new random value, and by chance it can match the one before.
How do you get two separate random values, like two dice?
Make two calls and store each result in its own variable. Copying one variable into another just copies the same number.

Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.

Learning with Lightbulb is opening soon

You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.

Keep me posted

More AQA GCSE Computer Science topics

See the full AQA Computer Science curriculum →

How this lesson was checked. This AQA GCSE Computer Science (specification 8525)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 29 September 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.