GoalUse random.randint(low, high) and say which values a program can and cannot produce.
New words
random.randint(a, b) · gives a whole number from a to b, including both ends, chosen by chancepossible values · all the results a call could giverandom library · the Python library with tools for chance; you import random to use it
Explanation
Games, raffles and simulations need results that nobody can predict. The random library provides them. After import random, the call random.randint(1, 6) gives one whole number from 1 to 6. Both ends are included. Each time the program runs, the number may be different. So you can never say what one run will print. What you can do is list the possible values, and say which values are impossible. If the number is stored in a variable, the rest of the program works with whatever number arrived. Use the first number for the smallest value and the second for the largest, and check that the order is right.
Call
Possible values
How many
random.randint(1, 4)
1, 2, 3, 4
4
random.randint(5, 6)
5, 6
2
random.randint(10, 14)
10, 11, 12, 13, 14
5
random.randint(7, 7)
7
1
Worked examples
Python
1import random
2ticket = random.randint(100, 104)
3print("Winning ticket:", ticket)
A raffle. The winning ticket is between 100 and 104. One run might print Winning ticket: 102, and another might print 100. You cannot know which. The only possible lines are Winning ticket: 100 up to Winning ticket: 104.
Python
1import random
2n = random.randint(1, 3)
3print(n * 10)
Using the number in a sum. n can be 1, 2 or 3, so n * 10 can only be 10, 20 or 30. Nothing else can be printed.
Random and if together. spin can be 1 to 8. Only 7 and 8 are greater than 6, so Bonus can be printed on 2 of the 8 possible spins and No bonus on the other 6. Either line can appear on any run.
Class activity
Home-made spinner (no computer). Cut a circle of card and divide it into 8 equal parts. Number the parts 1 to 8. Push a pencil through the middle and spin it, or use a paper clip and a pencil point as a pointer. Spin 24 times and write each result in a tally chart. Check that you never get 0 or 9. Say which number came most often, and whether you expected that. Then decide what a computer would write for the same spinner: random.randint(1, 8). If you have a computer or tablet, ask an adult to run it a few times.
Practice
1
What are all the possible values of random.randint(5, 9)? How many are there?
5, 6, 7, 8 and 9. That is 5 values, because both ends are included.
2
Can this program print 11? Can it print 15? List everything it can print.
Python
1import random
2n = random.randint(2, 4)
3print(n + 10)
It cannot print 11 or 15. n is 2, 3 or 4, so n + 10 is 12, 13 or 14. Those are the only possible results.
3
Write two lines that store a random hour from 1 to 12 in a variable called hour. Do not forget the library.
Model answer (lines are separated by /): import random / hour = random.randint(1, 12)
4
A pupil wants a die with the numbers 1 to 6 but wrote this program. What is wrong? What kind of error is it?
Python
1import random
2die = random.randint(1, 5)
3print("You rolled", die)
It is a logic error: the program runs, but the answer is not what was wanted. The number 6 can never be rolled because the highest value is 5. Change the call to random.randint(1, 6).