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.
import random
dice = random.randint(1, 6)
print(dice)- 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.
import random
first = random.randint(1, 6)
second = random.randint(1, 6)
total = first + second
print(first, second, total)- What is the smallest possible total?
- What is the largest possible total?
- Why can different runs produce different values?
Using randint()
import random
number = random.randint(10, 20)
print(number)import randommakes the module available.randint(10, 20)can produce any integer from 10 through 20.- Both endpoints are included.
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.
random(1,6) corresponds naturally to Python random.randint(1, 6), with both endpoints included.OCR Exam Reference Language ↔ Python
| OCR Exam Reference Language / term | Python | What 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 ERL | import random | Python must import the module before using random.randint. |
Worked example
Two dice require two separate random-number calls.
import random
die1 = random.randint(1, 6)
die2 = random.randint(1, 6)
total = die1 + die2
print(die1)
print(die2)
print(total)Example run: 4 Example run: 2 Example total: 6
- 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.
import random
roll = random.randint(1, 100)
if roll <= 10:
print("Rare")
elif roll <= 40:
print("Uncommon")
else:
print("Common")Output varies between runs
- The random value is generated before selection.
- The ordered conditions divide 1–100 into non-overlapping ranges.
Common mistakes
- Forgetting
import randomin 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.
random.randint(a, b) includes both a and b, matching the inclusive OCR integer example.Exam-ready checklist
- Generate a random integer in a stated range.
- Translate OCR
random(a,b)to Pythonrandom.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
randommodule 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.