[Python-ideas] random.boolean or bernoulli

Raymond Hettinger raymond.hettinger at gmail.com
Thu Apr 21 05:55:05 CEST 2011


On Apr 20, 2011, at 8:49 PM, Stefan Behnel wrote:

> Erich Blume, 20.04.2011 23:37:
>> 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))
> 
> I like this one. It reads well in code:
> 
>    if random.boolean():
>        ...

The traditional way to spell it is:

   if random() < 0.5:
         ...


Raymond



More information about the Python-ideas mailing list