Suppose I have two functions:
functionA()
and functionB()
I do not care which function runs, but I do want just ONE of them to run randomly--ie, if I run the script a hundred times, both should be played near 50 times.
How can I program that into Python 2?
In Python, functions are first class citizens so you can put them in a list and then randomly select one of them at every turn using random.choice
:
>>> import random
>>> functions = [functionA, functionB]
>>> for _ in range(100):
... function = random.choice(functions)
... function()
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