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 ← 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
- 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
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?
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 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.