Skip to the content

random with a seed

Python · Unit 16: The standard library

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. 1. Multiple choice

    Which values can random.randint(1, 6) give?

  2. 2. Complete the code

    Complete it to roll a die. It should print: True

  3. 3. Predict the output

    What does this code print?

  4. 4. Predict the output

    What does this code print?

  5. 5. Predict the output

    What does this code print?

  6. 6. Predict the output

    What does this code print?

  7. 7. Find the bug

    This program should print the shuffled cards. Which line has the error?

  8. 8. Complete the code

    Complete it so one of the names on the list comes out.

  9. 9. Multiple choice

    Why does die(3) == die(3) always give True?

  10. 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?

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →