[Python-ideas] random.boolean or bernoulli
Stephen J. Turnbull
stephen at xemacs.org
Thu Apr 21 19:02:00 CEST 2011
Joao S. O. Bueno writes:
> What about just:
>
> def chance(n=0.5):
> return random() < n
n=0.5 just isn't that special. Anyway, EIBTI.
Also, I almost certainly wouldn't bother to use random.chance() if it
existed. I'd simply use application-specific definitions like
def random_gender():
return 'female' if random() < p else 'male'
(it's quite rare that I actually want True and False as the values of
chance()-like functions).
> As for "unnecessary baggage" --about half the random module -
> randrange, randint, uniform, choice,could be viewed as "unecessary
> baggae"
Sure, this is always in the eye of the beholder. My taste is that the
module gets it about right. I've never needed the full functionality
of randrange, but I can imagine others using it fairly frequently, and
if I did need it it's cheaper to look it up than to code it up. I do
often use "choice" and somewhat less often "shuffle". These are
somewhat tedious to implement.
OTOH, to a mathematician, random() immediately makes one want to ask,
"what distribution?" So uniform() really is needed.
More information about the Python-ideas
mailing list