18-random-numbers.py
python-programming / further-programming-techniques  ·  Lesson 19
Lesson 19 — Further programming techniques

Random Number Generation

Generate unpredictable values for games, simulations and test data.

Python’s random module can generate random values. randint(a, b) returns an integer from a to b, inclusive.

Predict

Read the code first. Do not run it yet.

random-numbers-predict.py
import random
dice = random.randint(1, 6)
print(dice)
Predict before running
  • What is the smallest possible output?
  • What is the largest possible output?
  • Can you know the exact output before the code runs?

Investigate

Use this code to explain exactly how the program works.

random-numbers-investigate.py
import random
first = random.randint(1, 6)
second = random.randint(1, 6)
total = first + second
print(first, second, total)
Investigate the code
  • What is the smallest possible total?
  • What is the largest possible total?
  • Why can different runs produce different values?

Using randint()

random_number.py
import random
number = random.randint(10, 20)
print(number)
How it works
  • import random makes the module available.
  • randint(10, 20) can produce any integer from 10 through 20.
  • Both endpoints are included.
Inclusive endpointsFor random.randint(1, 6), both 1 and 6 are possible results.

Self-study guide

Random number generation lets a program produce values that are not fixed in advance. OCR Exam Reference Language uses random(lower, upper). In Python, this course uses the random module and random.randint(lower, upper) for inclusive random integers.

Randomness changes the data, not the fundamental programming logic. Generated values can be stored, compared, added to totals or used by selection just like any other integers.

OCR focusBe able to create and use random numbers in a program. For integer examples, know that OCR random(1,6) corresponds naturally to Python random.randint(1, 6), with both endpoints included.

OCR Exam Reference Language ↔ Python

OCR Exam Reference Language / termPythonWhat to remember
random(1,6)random.randint(1, 6)Random integer from 1 to 6 inclusive.
random(-1.0,10.0)random.uniform(-1.0, 10.0)Python provides uniform() for random real values; this course mainly uses integer randint().
no import shown in ERLimport randomPython must import the module before using random.randint.
Read both formsIn an examination you may see OCR Exam Reference Language even when you have learned the idea in Python. Translate the structure, not just the symbols.

Worked example

Two dice require two separate random-number calls.

lesson-19-worked.py
import random

die1 = random.randint(1, 6)
die2 = random.randint(1, 6)
total = die1 + die2

print(die1)
print(die2)
print(total)
Output
Example run: 4
Example run: 2
Example total: 6
Work through it line by line
  • The module is imported first.
  • Each call to randint(1, 6) can independently produce any integer from 1 through 6.
  • The random results are ordinary integers after generation.
  • Because the result changes, testing should include several runs rather than assuming one observed output is always produced.

Another example

Selection can interpret a random value.

lesson-19-example-2.py
import random

roll = random.randint(1, 100)

if roll <= 10:
    print("Rare")
elif roll <= 40:
    print("Uncommon")
else:
    print("Common")
Output
Output varies between runs
Why this example matters
  • The random value is generated before selection.
  • The ordered conditions divide 1–100 into non-overlapping ranges.

Common mistakes

Avoid these errors
  • Forgetting import random in Python.
  • Assuming the upper bound of randint() is excluded; it is included.
  • Writing OCR random(1,6) directly in Python without the module/function form used here.
  • Trying to predict one fixed output from a random program.
Exam-safe warningFor Python integer work in this course, random.randint(a, b) includes both a and b, matching the inclusive OCR integer example.

Exam-ready checklist

You should now be able to
  • Generate a random integer in a stated range.
  • Translate OCR random(a,b) to Python random.randint(a,b) for integer tasks.
  • Use random values inside calculations, loops and selection.
  • Explain why repeated tests may produce different results.

What you have learned

  • Import the random module before using it.
  • randint(a, b) generates an integer in the inclusive range a to b.
  • Random numbers are useful when a program should not always behave identically.