Random Library

Python does not contain all the functions we could possibly want to use, if it did it would run slowly and take up all the computer's memory.

Instead we use libraries to add extra features to python.

When we want to use a library we can import them to access the functions we need

In order to use the random library we need to ensure all of our programs have the following line at the top of our code:

import random

Once we have imported the random library we can generate random numbers using the following line of code

random.randint(1,6)

The brackets in this function must contain two numbers separated by a comma, these are the numbers between which the random number will be generated. The above will generate a number between 1 and 6 (including 1 and 6).

We can store the number inside a variable too if we need to use it later, or check which number has been generated

dice = random.randint(1,6)


Included in the following specifications:
KS3 Computing