From c83efa7a66e30276c328fa4a5f8f8d26977f3e1c Mon Sep 17 00:00:00 2001 From: dgpb <3577712+dg-pb@users.noreply.github.com> Date: Fri, 21 Mar 2025 00:07:28 +0200 Subject: gh-131435: random.randint optimization (gh-131436) --- Lib/random.py | 7 +++++-- .../next/Library/2025-03-19-20-37-07.gh-issue-131435.y8KMae.rst | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2025-03-19-20-37-07.gh-issue-131435.y8KMae.rst diff --git a/Lib/random.py b/Lib/random.py index 4d9a047..d6f5337 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -336,8 +336,11 @@ class Random(_random.Random): def randint(self, a, b): """Return random integer in range [a, b], including both end points. """ - - return self.randrange(a, b+1) + a = _index(a) + b = _index(b) + if b < a: + raise ValueError(f"empty range in randint({a}, {b})") + return a + self._randbelow(b - a + 1) ## -------------------- sequence methods ------------------- diff --git a/Misc/NEWS.d/next/Library/2025-03-19-20-37-07.gh-issue-131435.y8KMae.rst b/Misc/NEWS.d/next/Library/2025-03-19-20-37-07.gh-issue-131435.y8KMae.rst new file mode 100644 index 0000000..1a9810a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-03-19-20-37-07.gh-issue-131435.y8KMae.rst @@ -0,0 +1 @@ +10-20% performance improvement of :func:`random.randint`. -- cgit v0.12