49 – Random choices#
Predictability is usually helpful, but it can also be quite boring. On the other hand, randomness isn’t always helpful, but sometimes it’s the only way to get something done.
(As a real-world example, the generic profile pictures in the testimonials page on my website have random patterns and random colours.)
To pick a random value from a list, you have two alternatives:
Use
random.choicesif you want to pick values with replacement (values can be repeated):
import random
coin_sides = ["heads", "tails"]
print(random.choices(coin_sides, k=4))
# ['heads', 'tails', 'tails', 'tails']
Use
random.sampleif you want to pick values without replacement (values cannot be repeated):
import random
colours = ["red", "green", "blue",
"black", "white"]
print(random.sample(colours, k=3))
# ['black', 'green', 'blue']
For either, set k to specify how many values you want to draw from the given list.
Note: for security-sensitive randomness, use the module secrets; not the module random.