import random
print(random.random())
print(random.random())
print(random.random())
0.3637220567661291
0.23171211898083455
0.3725173462755005
random
ModuleThe 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:
= ["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: straw
FLAVOR: van
FLAVOR: van