Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-131435: random.randint optimization #131436

Merged
merged 2 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Lib/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 -------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10-20% performance improvement of :func:`random.randint`.
Loading