https://github.com/python/cpython/commit/c83efa7a66e30276c328fa4a5f8f8d26977f3e1c
commit: c83efa7a66e30276c328fa4a5f8f8d26977f3e1c
branch: main
author: dgpb <3577712+dg...@users.noreply.github.com>
committer: rhettinger <rhettin...@users.noreply.github.com>
date: 2025-03-20T17:07:28-05:00
summary:

gh-131435: random.randint optimization (gh-131436)

files:
A Misc/NEWS.d/next/Library/2025-03-19-20-37-07.gh-issue-131435.y8KMae.rst
M Lib/random.py

diff --git a/Lib/random.py b/Lib/random.py
index 4d9a047b027974..d6f5337d40f6ba 100644
--- a/Lib/random.py
+++ b/Lib/random.py
@@ -336,8 +336,11 @@ def randrange(self, start, stop=None, step=_ONE):
     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 00000000000000..1a9810a8fed7d5
--- /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`.

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to