I know that if I want to randomly generate a number I do something like this
import random
run = -1
for x in range(10):
rand = random.randint(1, 30)
but how can I get a random generation of yes or no instead of numbers?
You can do it very directly with choice from the standard module random.
>>> from random import choice
>>> answer = choice(['yes', 'no'])
>>> answer
'yes'
A simple coin toss would be something like this
def coin_toss(p=.5):
return 'yes' if random.random() < p else 'no'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With