Revise Computing
Revision notesQuestionsFor schools
Log inStart revising free
Revision notesQuestionsFor schoolsLog in
GCSE Computer Science/Supplementary programming practice/Random Number Generation

Off-by-one

301.14 Random Number Generation

Practise exam questions

Random Number Generation — Note 3 of 3

Avoid fencepost errors.

What you need to know

Off-by-one errors with random numbers happen when you use the wrong range boundaries. Remember that randint(a, b) includes both a and b.

What to know

  • randint(1, 6) includes both 1 and 6
  • randint(0, 9) gives 10 possible values (0 through 9)
  • Don't confuse with range() which excludes the end
  • Test with the minimum and maximum values to verify

Code examples

Off-by-one check
python
import random
# Want numbers 1 to 10 inclusive
num = random.randint(1, 10)  # Correct: includes 10

# Common mistake with range:
# range(1, 10) gives 1-9 (excludes 10)
# randint(1, 10) gives 1-10 (includes 10)
← Previous noteGames301.14 Random Number GenerationNext topic →Opening files301.15 File Handling Techniques

Back to Random Number Generation

Specification notes for programming are in Paper 2.

Random Number Generation

Note 3 of 3

  1. 1Random integers
  2. 2Games
  3. 3Off-by-one
Topic overview

© 2026 Revise Computing · revisecomputing.co.uk

Privacy noticeTermsContact

Revise Computing is an independent revision resource. It is not affiliated with, or endorsed by, OCR (Oxford Cambridge and RSA Examinations).