Repository navigation
gh-162817: Add randbool recipe to random documentation - #162836
George-Ogden wants to merge 6 commits into
Conversation
Documentation build overview
|
picnixz
left a comment
There was a problem hiding this comment.
The recipe is really really small and this could help but... at the same time I'm not sure if this warrants a new recipe. We could just enhance the random() entry as well saying that random.random() < p can be used to sample a boolean according to a Bernoulli distribution of parameter p assuming p in [0,1].
| >>> assert 480 <= sum(randbool() for _ in range(1000)) <= 520 | ||
| >>> assert 880 <= sum(randbool(0.9) for _ in range(1000)) <= 920 | ||
| >>> assert sum(randbool(0) for _ in range(10_000)) == 0 | ||
| >>> assert sum(randbool(1) for _ in range(10_000)) == 10_000 | ||
| >>> randbool(1.0) | ||
| True | ||
| >>> randbool(0.0) | ||
| False |
There was a problem hiding this comment.
Please make a new section, like for any other recpies and don't use assertions. Start with random.seed(8675309) and expose the exact results.
But again, I wonder whether we need a recipe here and not just a small note on random.random() instead.
| if all(p != q for p, q in zip(start, perm)): | ||
| return tuple([seq[i] for i in perm]) | ||
|
|
||
| def randbool(p = 0.5): |
There was a problem hiding this comment.
| def randbool(p = 0.5): | |
| def randbool(p=0.5): |
I think this recipe is so simple that it should be put first. But again I still think it's too simple and thus should be put in a random.random() note.
| To generate a random boolean value, use ``random() < p``. It is | ||
| ``True`` with probability ``p`` and ``False`` with probability ``1-p``. |
There was a problem hiding this comment.
| To generate a random boolean value, use ``random() < p``. It is | |
| ``True`` with probability ``p`` and ``False`` with probability ``1-p``. | |
| To generate a random boolean value with probability ``0.0 <= p <= 1.0``, | |
| use ``random() < p``. |
Fixes #162817
random.randboolfor Bernoulli distribution overbool#162817