[Python-ideas] random.boolean or bernoulli

Joao S. O. Bueno jsbueno at python.org.br
Thu Apr 21 17:30:07 CEST 2011


On Thu, Apr 21, 2011 at 8:46 AM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Erich Blume wrote:
>
>> def bernoulli(p):
>>    return random() <= p
>
> When I use a function like this I've been calling
> it chance(), which seems less jargony.
>
>> def boolean():
>>    return choice((True,False))
>
> Since this is equal to chance(0.5), I'm not sure
> it's worth it. A chance of exactly 50% seems like
> a rather special case.


What about  just:

def chance(n=0.5):
      return random() < n

?
As for "unnecessary baggage"  --about half the random module -
randrange, randint, uniform, choice,could be viewed as "unecessary
baggae" -- but the ability of cleamly specifying what one wants on the
random module through this functions, instead of having to deal with
the raw "random floating point from 0.0 to 1.0" as all other languages
is what probably made me start using Python, about 10 years ago.

  js
 -><-
>
> --
> Greg
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>



More information about the Python-ideas mailing list