[Python-ideas] random.boolean or bernoulli

Raymond Hettinger raymond.hettinger at gmail.com
Thu Apr 21 07:11:49 CEST 2011


On Apr 20, 2011, at 2:37 PM, Erich Blume wrote:

> 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.

ISTM, it would be better if you first gained some experience using the module as-is.


> 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,

I agree that they are unnecessary baggage.
AFAICT, other languages have avoided adding this sort of thing.
We already have randrange(), so this is just an inflexible specialization.
It is better to propose ideas that substantially increase the power of the module,
not ones that offer trivial respellings.


Raymond


More information about the Python-ideas mailing list