AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Rounding and rolling

Week 12 · 1.6 Library programs · 3 lessons
2

Numbers by chance

Computing 8: Topic 1.6, pp. 125–149
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.

CallPossible valuesHow many
random.randint(1, 4)1, 2, 3, 44
random.randint(5, 6)5, 62
random.randint(10, 14)10, 11, 12, 13, 145
random.randint(7, 7)71
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.
Python
1import random
2spin = random.randint(1, 8)
3if spin > 6:
4    print("Bonus")
5else:
6    print("No bonus")
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?
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)
3
Write two lines that store a random hour from 1 to 12 in a variable called hour. Do not forget the library.
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)