Skip to content

gh-162817: Add randbool recipe to random documentation - #162836

Open
George-Ogden wants to merge 6 commits into
python:mainfrom
George-Ogden:randbool-recipe
Open

George-Ogden wants to merge 6 commits into
python:mainfrom
George-Ogden:randbool-recipe

Conversation

@George-Ogden

@George-Ogden George-Ogden commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

@read-the-docs-community

read-the-docs-community Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #36243771 | 📁 Comparing 698e91a against main (31ed3ef)

  🔍 Preview build  

1 file changed
± library/random.html

@picnixz picnixz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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].

Comment thread Doc/library/random.rst Resolved
Comment thread Doc/library/random.rst Outdated
Comment on lines +758 to +765
>>> 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread Doc/library/random.rst Outdated
if all(p != q for p, q in zip(start, perm)):
return tuple([seq[i] for i in perm])

def randbool(p = 0.5):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Comment thread Doc/library/random.rst
Comment on lines +302 to +303
To generate a random boolean value, use ``random() < p``. It is
``True`` with probability ``p`` and ``False`` with probability ``1-p``.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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``.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review docs Documentation in the Doc dir skip news type-feature A feature request or enhancement

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

Add random.randbool for Bernoulli distribution over bool

2 participants