From 69c1af1daf867721052a7444f80b4aa3ec1bb210 Mon Sep 17 00:00:00 2001 From: George Ogden Date: Sun, 29 Nov 2026 12:43:20 +0000 Subject: [PATCH 1/8] Add randbool recipe to random documentation --- Doc/library/random.rst | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/Doc/library/random.rst b/Doc/library/random.rst index 35795cde95d56ff..50a4207d2e5193c 100644 --- a/Doc/library/random.rst +++ b/Doc/library/random.rst @@ -747,6 +747,36 @@ or the :pypi:`more-itertools` project: >>> random_derangement((10, identical, 30, identical)) (20, 30, 10, 20) +Another common use-case is to generate a random boolean value from a Bernoulli distribution. This recipe defaults to a uniform distribution when *p* is not specified. + +.. testcode:: + def randbool(p = 0.5): + """Return True with probability p and False with probability 1-p.""" + if not 0.0 <= p <= 1.0: + raise ValueError(f"Random value should between 0 and 1, got {p}.") + return random.random() < p + +.. doctest:: + :hide: + + >>> import random + + >>> random.seed(0) + >>> 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 + >>> randbool(1.1) + ValueError() + >>> randbool(-0.1) + ValueError() + >>> import math + >>> randbool(math.nan) + ValueError() The default :func:`.random` returns multiples of 2⁻⁵³ in the range *0.0 ≤ x < 1.0*. All such numbers are evenly spaced and are exactly From 39f801dc8015bd3670ec66ae3b4715b46d8ce5fa Mon Sep 17 00:00:00 2001 From: George Ogden Date: Sun, 29 Nov 2026 12:53:20 +0000 Subject: [PATCH 2/8] Wrap documentation paragraph to 80 characters --- Doc/library/random.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Doc/library/random.rst b/Doc/library/random.rst index 50a4207d2e5193c..d51703bf6365f25 100644 --- a/Doc/library/random.rst +++ b/Doc/library/random.rst @@ -747,7 +747,9 @@ or the :pypi:`more-itertools` project: >>> random_derangement((10, identical, 30, identical)) (20, 30, 10, 20) -Another common use-case is to generate a random boolean value from a Bernoulli distribution. This recipe defaults to a uniform distribution when *p* is not specified. +Another common use-case is to generate a random boolean value from a Bernoulli +distribution. This recipe defaults to a uniform distribution when *p* is not +specified. .. testcode:: def randbool(p = 0.5): From ba6afe4c17e087e292bb0aed1fbf263d6d2a070a Mon Sep 17 00:00:00 2001 From: George Ogden Date: Sun, 29 Nov 2026 12:53:38 +0000 Subject: [PATCH 3/8] Indent code with 4 spaces --- Doc/library/random.rst | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Doc/library/random.rst b/Doc/library/random.rst index d51703bf6365f25..daff0889a17dcd5 100644 --- a/Doc/library/random.rst +++ b/Doc/library/random.rst @@ -752,11 +752,13 @@ distribution. This recipe defaults to a uniform distribution when *p* is not specified. .. testcode:: + import random + def randbool(p = 0.5): - """Return True with probability p and False with probability 1-p.""" - if not 0.0 <= p <= 1.0: - raise ValueError(f"Random value should between 0 and 1, got {p}.") - return random.random() < p + """Return True with probability p and False with probability 1-p.""" + if not 0.0 <= p <= 1.0: + raise ValueError(f"Random value should between 0 and 1, got {p}.") + return random.random() < p .. doctest:: :hide: From 336babd6d72e15d0a3c536af83471046482b6332 Mon Sep 17 00:00:00 2001 From: George Ogden Date: Sun, 29 Nov 2026 13:06:04 +0000 Subject: [PATCH 4/8] Fix doctests --- Doc/library/random.rst | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/Doc/library/random.rst b/Doc/library/random.rst index daff0889a17dcd5..004bfb5b8b5caae 100644 --- a/Doc/library/random.rst +++ b/Doc/library/random.rst @@ -752,12 +752,13 @@ distribution. This recipe defaults to a uniform distribution when *p* is not specified. .. testcode:: + import random def randbool(p = 0.5): """Return True with probability p and False with probability 1-p.""" if not 0.0 <= p <= 1.0: - raise ValueError(f"Random value should between 0 and 1, got {p}.") + raise ValueError(f"Random value should between 0 and 1, got {p!r}.") return random.random() < p .. doctest:: @@ -774,13 +775,19 @@ specified. True >>> randbool(0.0) False - >>> randbool(1.1) - ValueError() + >>> randbool(1.01) + Traceback (most recent call last): + ... + ValueError: Random value should between 0 and 1, got 1.01. >>> randbool(-0.1) - ValueError() + Traceback (most recent call last): + ... + ValueError: Random value should between 0 and 1, got -0.1. >>> import math >>> randbool(math.nan) - ValueError() + Traceback (most recent call last): + ... + ValueError: Random value should between 0 and 1, got nan. The default :func:`.random` returns multiples of 2⁻⁵³ in the range *0.0 ≤ x < 1.0*. All such numbers are evenly spaced and are exactly From 7eb28cf942a0665f782a68e12dce184230de63e0 Mon Sep 17 00:00:00 2001 From: George Ogden Date: Sun, 29 Nov 2026 14:06:50 +0000 Subject: [PATCH 5/8] Shorten randbool recipe documentation --- Doc/library/random.rst | 43 +++++++++--------------------------------- 1 file changed, 9 insertions(+), 34 deletions(-) diff --git a/Doc/library/random.rst b/Doc/library/random.rst index 004bfb5b8b5caae..23eef8be75d1118 100644 --- a/Doc/library/random.rst +++ b/Doc/library/random.rst @@ -677,6 +677,15 @@ or the :pypi:`more-itertools` project: if all(p != q for p, q in zip(start, perm)): return tuple([seq[i] for i in perm]) + def randbool(p = 0.5): + """ + Generate a random boolean from a Bernoulli distribution. + Returns True with probability p and False with probability 1-p. + """ + if not 0.0 <= p <= 1.0: + raise ValueError(f"Random value should between 0 and 1, got {p!r}.") + return random.random() < p + .. doctest:: :hide: @@ -746,27 +755,6 @@ or the :pypi:`more-itertools` project: >>> identical = 20 >>> random_derangement((10, identical, 30, identical)) (20, 30, 10, 20) - -Another common use-case is to generate a random boolean value from a Bernoulli -distribution. This recipe defaults to a uniform distribution when *p* is not -specified. - -.. testcode:: - - import random - - def randbool(p = 0.5): - """Return True with probability p and False with probability 1-p.""" - if not 0.0 <= p <= 1.0: - raise ValueError(f"Random value should between 0 and 1, got {p!r}.") - return random.random() < p - -.. doctest:: - :hide: - - >>> import random - - >>> random.seed(0) >>> 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 @@ -775,19 +763,6 @@ specified. True >>> randbool(0.0) False - >>> randbool(1.01) - Traceback (most recent call last): - ... - ValueError: Random value should between 0 and 1, got 1.01. - >>> randbool(-0.1) - Traceback (most recent call last): - ... - ValueError: Random value should between 0 and 1, got -0.1. - >>> import math - >>> randbool(math.nan) - Traceback (most recent call last): - ... - ValueError: Random value should between 0 and 1, got nan. The default :func:`.random` returns multiples of 2⁻⁵³ in the range *0.0 ≤ x < 1.0*. All such numbers are evenly spaced and are exactly From 698e91a1958dcbdd80431df1f6c418e7d3ca95cf Mon Sep 17 00:00:00 2001 From: George Ogden Date: Sun, 29 Nov 2026 14:38:35 +0000 Subject: [PATCH 6/8] Add note on random function about generating boolean values --- Doc/library/random.rst | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/Doc/library/random.rst b/Doc/library/random.rst index 23eef8be75d1118..232f1bc797f1952 100644 --- a/Doc/library/random.rst +++ b/Doc/library/random.rst @@ -299,6 +299,8 @@ be found in any statistics text. .. function:: random() Return the next random floating-point number in the range ``0.0 <= X < 1.0`` + To generate a random boolean value, use ``random() < p``. It is + ``True`` with probability ``p`` and ``False`` with probability ``1-p``. .. function:: uniform(a, b) @@ -677,15 +679,6 @@ or the :pypi:`more-itertools` project: if all(p != q for p, q in zip(start, perm)): return tuple([seq[i] for i in perm]) - def randbool(p = 0.5): - """ - Generate a random boolean from a Bernoulli distribution. - Returns True with probability p and False with probability 1-p. - """ - if not 0.0 <= p <= 1.0: - raise ValueError(f"Random value should between 0 and 1, got {p!r}.") - return random.random() < p - .. doctest:: :hide: @@ -755,14 +748,7 @@ or the :pypi:`more-itertools` project: >>> identical = 20 >>> random_derangement((10, identical, 30, identical)) (20, 30, 10, 20) - >>> 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 + The default :func:`.random` returns multiples of 2⁻⁵³ in the range *0.0 ≤ x < 1.0*. All such numbers are evenly spaced and are exactly From 7963bc5309be7409e0bab4a96af1ad3b4c44ee5a Mon Sep 17 00:00:00 2001 From: George Ogden <38294960+George-Ogden@users.noreply.github.com> Date: Sun, 29 Nov 2026 19:50:08 +0000 Subject: [PATCH 7/8] Reword random boolean sample description MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Doc/library/random.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Doc/library/random.rst b/Doc/library/random.rst index 232f1bc797f1952..a682026c97baeaf 100644 --- a/Doc/library/random.rst +++ b/Doc/library/random.rst @@ -299,8 +299,9 @@ be found in any statistics text. .. function:: random() Return the next random floating-point number in the range ``0.0 <= X < 1.0`` - 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``. .. function:: uniform(a, b) From 14544df1b312e84f7a4cc5d4d959b9dc1b9572c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sun, 29 Nov 2026 22:08:11 +0100 Subject: [PATCH 8/8] Update Doc/library/random.rst --- Doc/library/random.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/random.rst b/Doc/library/random.rst index a682026c97baeaf..0704ff1d06ac379 100644 --- a/Doc/library/random.rst +++ b/Doc/library/random.rst @@ -300,7 +300,7 @@ be found in any statistics text. Return the next random floating-point number in the range ``0.0 <= X < 1.0`` - To generate a random boolean value with probability ``0.0 <= p <= 1.0``, + To generate :const:`True` with probability ``0.0 <= p <= 1.0``, use ``random() < p``.