GCSE · Computer Science · OCR · Spec J277
Random number generation
Dice games, guessing games, rock, paper, scissors: they all need a number nobody chose. Python makes one in a line. But could it guard a secret?
Computer Science · Random numbers
Roll a dice in five lines
Step through a tiny dice program. Watch where the number comes from, where it gets stored, and what gets printed.
Before you step: could roll ever end up holding 6? What about 0?
1 from random import randint2 roll = randint(1, 6)3 print("You rolled", roll)4 roll = randint(1, 6)5 print("You rolled", roll)
| roll |
|---|
Output
Step 1: Nothing has been rolled yet. This line just fetches the randint function from the random module, so the program can use it.
Watch the roll column: it changes only on the lines that call randint.
Predict, then check
Commit before you peek. Your gut feeling is exactly what we're testing.
A quiz app picks a question with q = randint(1, 5). Can q ever be 5?
Computer Science · Pseudo vs true random
Good enough, or not good enough?
Which kind of random number generator does each job need?
Still to sort
A pseudo random generator is fine (0)
Jobs that aren't about security, like games.
Where the line is: Predictable numbers do no harm here, because nothing is being kept secret.
True random generation needed (0)
Jobs about security: keeping data secret.
Where the line is: If someone working out the numbers would expose a secret, a predictable algorithm is not good enough.
Sort each job. Ask one question: is anything being kept secret?
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
One line of Python can roll a dice. By the end you'll know how to write it, which numbers it can really give you, and when that kind of random just isn't good enough.
What you need to know
- A module is a file of pre-written Python code that you can use in other programs.
- A library is a collection of related modules. Python's standard library is the collection of built-in modules that come with Python.
- Modules save you time, keep code cleaner and easier to read, and give you features like random numbers, dates and maths functions.
- The randint function, from the random module, gives a random whole number between the two values you give it.
- Both ends count: randint(1, 5) can give 1, 2, 3, 4 or 5.
Have a goYour app needs to pick a month at random, numbered 1 to 12. Write the randint call before you look.
randint(1, 12)
Both ends are included, so 1 and 12 go in exactly as they are. Writing randint(1, 13) to 'make room' for 12 would let a month 13 sneak in.
- Store the result in a variable, like roll = randint(1, 6), so your program can use it.
- Import just the function with from random import randint, or import the whole module and call random.randint(1, 5).
Have a goYour program starts with from random import randint. Finish the line that stores a random number from 1 to 100 in score.
score = randint(1, 100)
After from random import randint you call randint directly. The random. prefix belongs with the other style, import random.
- Pseudo random number generators use mathematical algorithms. Their numbers are computer generated, so they're highly predictable.
- True random number generators use unpredictable physical means, such as atmospheric noise, so they suit cryptography: keeping data secret.
- Unless the numbers are for security, like in a computer game, a pseudo random generator is fine.
Have a goYour friend's space game uses a pseudo random generator to decide where asteroids appear. He wants to rebuild it with atmospheric noise 'so it's properly random'. Worth the weekend?
No. Pseudo random is fine for a game.
True random generation matters when numbers protect something secret. Asteroids aren't a secret, so the predictable algorithm does no harm here.
The big picture
Python's random module contains randint, which gives a random whole number between two values, including both of them. You can import just the function (from random import randint) or the whole module (import random, then random.randint). Modules and libraries save you writing code yourself. Numbers made by a mathematical algorithm are pseudo random: fine for games, but highly predictable. Anything about keeping data secret needs true random numbers from an unpredictable physical source, such as atmospheric noise.
Key points
Worked example
Problem
A board game needs two dice. Write a program that rolls two dice (each 1 to 6), stores each roll in its own variable and outputs the total. Then decide: is a pseudo random number generator good enough here?
⚠ Watch out
Thinking randint(1, 5) stops at 4. Both values you give it are included, so 5 can come out too. The other classic slip: writing import random and then calling plain randint(…). With that import, the call has to be random.randint(…).
Memory hook
Good enough for dice, not good enough for secrets. And randint keeps both ends: 1 to 6 means 1 AND 6.
Check yourself
Cover the page. Import only randint, then store a random whole number from 10 to 20 in level. Why is pseudo random fine here, but not for keeping data secret?
Flashcards
(15)What is a module?
What is a library?
What is the Python standard library?
Three benefits of using modules?
Which module contains randint?
What does randint(a, b) give you?
List every value randint(3, 7) could return.
How do you keep a randint result to use later?
Which line imports only the randint function?
After import random, how must you call randint?
How does a pseudo random number generator make numbers?
What does 'highly predictable' mean for pseudo random numbers?
How does a true random number generator make numbers?
When do you need true random numbers rather than pseudo random ones?
Three ways computer games use random numbers?
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 OCR GCSE Computer Science topics
How this lesson was checked. This OCR GCSE Computer Science (specification J277)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 3 November 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.