Drop hier links of afbeeldingen om ze aan de editor toe te voegen.

Every program you have written so far does exactly the same thing every time you run it with the same input. That is usually what you want. It is not what you want from a dice game, a quiz that picks its questions in a different order, or a guessing game where the player has to find a number nobody told them.

For those, the program needs a number that nobody chose in advance: a random number. Python can produce one, but the tools for it are not switched on until you ask for them.

Importing the random module

Python keeps its random number tools in a module called random: a collection of ready-made code that comes with Python but is not loaded until a program asks for it. The program asks with an import line, and that line goes at the very top:

import random

After that line, everything in the module can be used by writing random, a full stop, and the name of the tool you want.

A random whole number

The tool for a random whole number is randint, short for random integer:

import random

number = random.randint(1, 10)
print(number)

Run that and it prints a number from 1 to 10. Run it again and it will very probably print a different one.

randint needs two numbers in its brackets, separated by a comma:

Values handed to a function in its brackets like this are called arguments. print("Hello") has one argument, and random.randint(1, 10) has two.

Both ends are included. random.randint(1, 10) can pick 1, it can pick 10, and it can pick everything in between: ten possible numbers.

That is not how range works. range(1, 10) stops before 10, while random.randint(1, 10) can land on 10. It is worth saying that out loud once, because the two look so alike.

Rolling a dice

A dice has six sides, numbered 1 to 6, so a program that rolls one picks a random whole number from 1 to 6:

import random

print("Welcome to the dice simulator!")
num1 = random.randint(1, 6)
print("You rolled a " + str(num1))

Run it several times. Each run is a new roll.

The random number is stored in a variable like any other number, and like any other number it has to be wrapped in str() before it can be joined onto text with a +.

Using the number

Once the random number is in a variable, the program does not care where it came from. It can be compared, added up, used in an if or used to decide when a loop stops, exactly as if the user had typed it:

import random

roll = random.randint(1, 6)
print("You rolled a " + str(roll))

if roll == 6:
    print("A six! Roll again.")
else:
    print("Next player's turn.")

Keep running it until you see both messages.

Help! My code doesn't work!

Make sure that you check for the following things:

  • import random is the first line of the program.
  • randint is written after random and a full stop: random.randint(...).
  • There are two numbers in the brackets, separated by a comma, lowest first.
  • Both numbers can be picked, so random.randint(1, 6) includes the 6.
  • The random number is wrapped in str() before it is joined onto text with a +.