Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c83efa7

Browse files
authoredMar 20, 2025
gh-131435: random.randint optimization (gh-131436)
1 parent ce79274 commit c83efa7

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed
 

‎Lib/random.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,11 @@ def randrange(self, start, stop=None, step=_ONE):
336336
def randint(self, a, b):
337337
"""Return random integer in range [a, b], including both end points.
338338
"""
339-
340-
return self.randrange(a, b+1)
339+
a = _index(a)
340+
b = _index(b)
341+
if b < a:
342+
raise ValueError(f"empty range in randint({a}, {b})")
343+
return a + self._randbelow(b - a + 1)
341344

342345

343346
## -------------------- sequence methods -------------------
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
10-20% performance improvement of :func:`random.randint`.

0 commit comments

Comments
 (0)
Failed to load comments.