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 randint
2 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.

1 / 5

Watch the roll column: it changes only on the lines that call randint.

Step 1 of 5: Nothing has been rolled yet. This line just fetches the randint function from the random module, so the program can use it..

Exam line: To use randint: import it, call it with two values, store the result in a variable.
Watch out: This is just one possible run. Run the same program again and roll could hold any whole number from 1 to 6 each time.

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 · Debugging

Find the broken line

Mo wants his guessing game to pick a secret number from 1 to 10. It won't work. Which line is wrong?

Mo's program — which line goes wrong?

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.

5 of 5 still to sort.

Sort each job. Ask one question: is anything being kept secret?

Watch out: 'Highly predictable' doesn't mean you can guess your next dice roll. It means the numbers come from a mathematical algorithm, so anyone who knows that algorithm and where it started could work out the numbers it will produce.

Computer Science · Modules and libraries

Your turn to write

Explain what a module is and what a library is. Then give two benefits of using modules in your programs. [4 marks]

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

Exam line: Define each term separately, then give two different benefits. One benefit said twice in different words still only counts once.

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

1randint(a, b) returns a random whole number from a to b, with a and b both possible.
2Import and call must match: from random import randint → randint(…); import random → random.randint(…).
3Keep the number by assigning it to a variable, then use that variable.
4Module = one file of ready-made code; library = a collection of related modules.
5Pseudo random (an algorithm, highly predictable) suits games; true random (physical, unpredictable) suits keeping data secret.

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?
A file containing pre-written Python code that can be used in other programs.
What is a library?
A collection of related modules.
What is the Python standard library?
A collection of built-in modules that come with Python.
Three benefits of using modules?
Saves time (no need to write the code yourself); cleaner, easier-to-read code; access to features like random numbers, dates and maths functions.
Which module contains randint?
The random module.
What does randint(a, b) give you?
A random whole number between a and b, including both a and b.
List every value randint(3, 7) could return.
3, 4, 5, 6 or 7.
How do you keep a randint result to use later?
Store it in a variable, e.g. dice = randint(1, 6).
Which line imports only the randint function?
from random import randint. Then call it as randint(…).
After import random, how must you call randint?
Through the module name: random.randint(…).
How does a pseudo random number generator make numbers?
With mathematical algorithms: the numbers are computer generated, so they are highly predictable.
What does 'highly predictable' mean for pseudo random numbers?
They come from an algorithm, so someone who knows the algorithm and its starting point could work them out.
How does a true random number generator make numbers?
From unpredictable physical means, such as atmospheric noise.
When do you need true random numbers rather than pseudo random ones?
For security purposes, such as cryptography (keeping data secret).
Three ways computer games use random numbers?
Dice games, choosing a random word in a word guessing game, and playing rock, paper, scissors against the computer.

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 OCR GCSE Computer Science topics

See the full OCR Computer Science curriculum →

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.