The
random module contains a consistent and advanced set of functions to generate
random numbers.
The randint Function
One of these functions saves us from the previous calculations to obtain integers.
The
randint(start, end) function returns a random integer (hence of type
int)
from the interval specified by the parameters.
The choice Function
We can choose one of the objects stored in a list or a string using the
choice(data_collection) function.
The function randomly returns an element from the object:
The shuffle Function
Suppose we have a list and we want to shuffle it, like a new deck of playing cards,
so that the default order of elements is randomly changed.
We can use the
shuffle(data_collection) function, which takes as a parameter
an object of type list or string. This function does not return anything but has a direct effect on the object passed as a parameter:
It does not work for the online interpreter, so test it on your computer in Python IDLE!
Proceed to the next page.