301.14 Random Number Generation
Practise exam questionsRandom Number Generation — Note 1 of 3
Generate numbers in range.
Use random.randint(a, b) to generate a random integer between a and b inclusive. You must import random first.
import random at the top of the programrandom.randint(1, 6) gives 1, 2, 3, 4, 5, or 6import random
dice = random.randint(1, 6)
print("You rolled: " + str(dice))
coin = random.randint(0, 1)
print("Heads" if coin == 1 else "Tails")Back to Random Number Generation
Specification notes for programming are in Paper 2.