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

Random integers

301.14 Random Number Generation

Practise exam questions

Random Number Generation — Note 1 of 3

Generate numbers in range.

What you need to know

Use random.randint(a, b) to generate a random integer between a and b inclusive. You must import random first.

What to know

  • import random at the top of the program
  • random.randint(1, 6) gives 1, 2, 3, 4, 5, or 6
  • Both endpoints are included (inclusive)
  • Each call gives a different random number

Code examples

Random integers
python
import random

dice = random.randint(1, 6)
print("You rolled: " + str(dice))

coin = random.randint(0, 1)
print("Heads" if coin == 1 else "Tails")
← Previous topicDebugging301.13 TestingNext note →Games301.14 Random Number Generation

Back to Random Number Generation

Specification notes for programming are in Paper 2.

Random Number Generation

Note 1 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).