random with a seed
Python · Unit 16: The standard library
- Full plan
- Python
- 10 exercises
The random module picks at random. random.randint(a, b) gives an integer between a and b, and both ends can come up.
Look at random.seed(7): without that line, every run would give a different number. With it, the same one always comes up. We'll see why in a moment.
import random
random.seed(7)
print(random.randint(1, 6))Prints
3
The rest of the explanation is in the lesson, which is part of the full plan.
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Multiple choice
Which values can
random.randint(1, 6)give?2. Complete the code
Complete it to roll a die. It should print: True
3. Predict the output
What does this code print?
4. Predict the output
What does this code print?
5. Predict the output
What does this code print?
6. Predict the output
What does this code print?
7. Find the bug
This program should print the shuffled cards. Which line has the error?
8. Complete the code
Complete it so one of the names on the list comes out.
9. Multiple choice
Why does
die(3) == die(3)always giveTrue?10. Find the case that fails
raffle(names, seed)picks a winner from the list, always the same one for the same seed, and returns "nobody" if there are no names. Which call breaks it?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.