pythonscientific-computing

True or false output based on a probability


Is there a standard function for Python which outputs True or False probabilistically based on the input of a random number from 0 to 1?

example of what I mean:

def decision(probability):
    ...code goes here...
    return ...True or False...

the above example if given an input of, say, 0.7 will return True with a 70% probability and false with a 30% probability


Solution

  • import random
    
    def decision(probability):
        return random.random() < probability