Revise Computingrevisecomputing.co.uk
At a glanceFeaturesStudentsPricingHow it worksFree GCSE notesExam dates
At a glanceFeaturesStudentsPricingHow it worksFree GCSE notesExam dates

Knowledge organisers / Random Number Generation

Random integers

All topicsPractise exam questions
Knowledge organiser

Random Number Generation

301.14a

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.

Key points

  • 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")