import random
print(random.random())
print(random.random())
print(random.random())
0.3398865601219281
0.37485856117083927
0.9604450235985988
random
ModuleThe 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:
= ["van", "choc", "straw"]
options
= random.choice(options)
flavor print("FLAVOR:", flavor)
= random.choice(options)
flavor print("FLAVOR:", flavor)
= random.choice(options)
flavor print("FLAVOR:", flavor)
FLAVOR: choc
FLAVOR: choc
FLAVOR: straw