True or false output based on a probability

Solution 1:

import random

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

Solution 2:

Given a function rand that returns a number between 0 and 1, you can define decision like this:

bool decision(float probability)
{
   return rand()<probability;
}

Assuming that rand() returns a value in the range [0.0, 1.0) (so can output a 0.0, will never output a 1.0).

Solution 3:

Just use PyProbs library. It is very easy to use.

>>> from PyProbs import Probability as pr
>>> 
>>> # You can pass float (i.e. 0.5, 0.157), int (i.e. 1, 0) or str (i.e. '50%', '3/11')
>>> pr.Prob(50/100)
False
>>> pr.Prob(50/100, num=5)
[False, False, False, True, False]