Appendix G — The random Module

The random module contains randomness related functions.

Random number generator:

import random

print(random.random())
print(random.random())
print(random.random())
0.3398865601219281
0.37485856117083927
0.9604450235985988

Random integer between lower and upper bound:

print(random.randint(1, 100))
print(random.randint(1, 100))
print(random.randint(1, 100))
36
93
12

Choose an item from a list at random:

options = ["van", "choc", "straw"]

flavor = random.choice(options)
print("FLAVOR:", flavor)

flavor = random.choice(options)
print("FLAVOR:", flavor)

flavor = random.choice(options)
print("FLAVOR:", flavor)
FLAVOR: choc
FLAVOR: choc
FLAVOR: straw