GCSE · Computer Science · AQA · Spec 8525
Random number generation
A dice game rolls six, six, six, six. Nothing's loaded: the code asks for a random number just once, in the wrong place. Soon you'll spot that bug in seconds.
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 ← 02 FOR turn ← 1 TO 33 roll ← RANDOM_INT(1, 6)4 OUTPUT roll5 IF roll = 6 THEN6 sixes ← sixes + 17 ENDIF8 ENDFOR9 OUTPUT "Sixes: ", sixes
Variables
Output
Step 1: sixes starts at 0. It will count how many sixes get rolled.
Press Next and watch roll. It only changes on line 3, the line with the random call.
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
- A random-number function hands your program a value it can't know in advance.
- RANDOM_INT(1, 6) gives a whole number from 1 to 6, and both 1 and 6 can come up.
Have a goQuick count before you look: how many different values can RANDOM_INT(10, 20) give?
11: every whole number from 10 to 20.
Both ends count, so it's 20 − 10 + 1. Doing just 20 − 10 = 10 quietly drops one of the ends.
- Languages write this differently, and some leave the top number out, so check what yours can give.
- Store the result in a variable, like roll ← RANDOM_INT(1, 6), so later lines can use it.
- Using a variable doesn't re-roll it: it keeps its value until something new is stored in it.
Have a goYour friend writes lucky ← RANDOM_INT(1, 10), then IF lucky > 5 THEN OUTPUT lucky. They're convinced the OUTPUT might show a different number from the one the IF just checked. Are they right?
No. The IF and the OUTPUT both read the same stored value.
Reading a variable never changes it. Only a new call stored in lucky could give it a new number, and there's only one call here.
- Every call makes a new random value, which could still match the last one by chance.
- Want a fresh value on every pass of a loop? Put the call inside the loop.
Have a goA loop runs 4 times, with one random call inside it. How many times does the random call actually run?
4 times: once per pass.
The call runs every time the loop body runs. Answering "once, because it's written once" counts lines of code, not times the line is run.
- Two separate random values, like two dice, need two calls, not one call and a copy.
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
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 call is right there, so it feels like it keeps working. It doesn't: the variable keeps that first value, so every pass uses it. 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?
Why store a random number in a variable?
Does a variable holding a random number change each time you use it?
You want a fresh random value on every pass of a loop. Where does the call go?
Does a new call always give a different number from the last one?
How do you get two separate random values, like two dice?
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 postedMore AQA GCSE Computer Science topics
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 October 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.