Knowledge organisersRandom Number Generation
Avoid fencepost errors.
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.
randint(1, 6) includes both 1 and 6randint(0, 9) gives 10 possible values (0 through 9)range() which excludes the endimport 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)