From: dgpb <3577712+dg-pb@users.noreply.github.com> Date: Thu, 20 Mar 2025 22:07:28 +0000 (+0200) Subject: gh-131435: random.randint optimization (gh-131436) X-Git-Tag: v3.14.0a7~269 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=c83efa7a66e30276c328fa4a5f8f8d26977f3e1c;p=thirdparty%2FPython%2Fcpython.git gh-131435: random.randint optimization (gh-131436) --- diff --git a/Lib/random.py b/Lib/random.py index 4d9a047b0279..d6f5337d40f6 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 000000000000..1a9810a8fed7 --- /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`.