Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Random Numbers

Games need chance: a die decides how far you move, and the cards are dealt differently every time. The module random gives your programs random numbers:

import random

print(random.randint(1, 6))
print(random.randint(1, 6))
print(random.choice(["rock", "paper", "scissors"]))

The output changes from run to run. One run printed:

2
4
paper

and the next one:

2
2
scissors
  • random.randint(1, 6) gives a random whole number from 1 to 6, like a die. Unlike range(), it includes both numbers: it can give 1, and it can give 6. It needs both of them, so random.randint(6) ends with TypeError: Random.randint() missing 1 required positional argument: 'b'.
  • random.choice() picks a random element of a list.

How can a computer, which only calculates, give random numbers? It calculates them. From a starting number, the seed, it works out a long series of numbers that look random. Python normally picks the seed itself, without telling you, and so the numbers come out different on every run. With random.seed(), you choose the seed yourself, and the same seed always gives the same series:

import random

random.seed(42)
print(random.randint(1, 6))
print(random.randint(1, 6))
print(random.choice(["rock", "paper", "scissors"]))
6
1
rock

Run this program as often as you like: it prints 6, 1 and rock every time. A game leaves the seed out, otherwise every game would roll the same dice. A seed is for when you need the same numbers again, for example to test a program. That's why the exercises in this lesson set one: so that they can check what your program prints.

With a seed, each result depends on all the calls before it, in their order. Here, the first randint(1, 6) gives 6 and the second one 1, but with another call before them, both would change. So in these exercises, make exactly the calls the instructions ask for, in that order.

Instructions

  1. 1

    A board game is about to start. Below the players, roll the die: print Dice: and a number from random.randint(1, 6). With the seed 12, the output is Dice: 4. Keep the line random.seed(12) as it is: the exercise also tries your program with other seeds.

  2. 2

    Below, pick who starts the game: print Starts: and one of the players, chosen with random.choice(). The output should be Starts: Eva. Once it passes, put a # in front of random.seed(12) and run the program a few times: now the die and the first player change from run to run, as in a real game.

Back

6/8

  1. 1. Importing a Module
  2. 2. Rounding
  3. 3. Quiz: Rounding
  4. 4. Importing with from
  5. 5. Quiz: Imports
  6. 6. Random Numbers
  7. 7. Average and Median
  8. 8. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.