[Python-ideas] random.boolean or bernoulli

Erich Blume blume.erich at gmail.com
Wed Apr 20 23:37:14 CEST 2011


I am yet a novice python user, and not a strong statistician to boot,
but I had an idea about how to enhance the 'random' module with one or
two new functions for generating boolean values instead of
floating-point values.

This idea has a lot of flexibility in how it might be implemented, but
I propose two new functions for the random module that might be
implemented in python as follows

def boolean():
    return choice((True,False))

def bernoulli(p):
    return random() <= p

It's true that since both of these functions have very simple
short-statement implementations that it might be unnecessary baggage,
but it occurred to me that their implementation might be consistent
with the rest of the module.

~eblume



More information about the Python-ideas mailing list