from random import random def bernoulli(p): if random()>> bernoulli(0.4) 1 >>> bernoulli(0.8) 0