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.3637220567661291
0.23171211898083455
0.3725173462755005

Random integer between lower and upper bound:

print(random.randint(1, 100))
print(random.randint(1, 100))
print(random.randint(1, 100))
31
30
80

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: straw
FLAVOR: van
FLAVOR: van