[Python-ideas] random.boolean or bernoulli

Stefan Behnel stefan_ml at behnel.de
Thu Apr 21 06:20:10 CEST 2011


Raymond Hettinger, 21.04.2011 05:55:
> 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:
>           ...

When I see constructs like this, my first thought is "Is there an 
off-by-one error here?", which then distracts my reading. It obviously 
wouldn't even matter here, since the randomness properties of random() are 
likely not good enough to see any difference, but that's second thought to 
me. It starts off by getting in my way.

Stefan




More information about the Python-ideas mailing list