Skip to content

Commit 26a1ad1

Browse files
authored
Small clean-ups for the random module (GH-21038)
1 parent a16d697 commit 26a1ad1

File tree

1 file changed

+27
-30
lines changed

1 file changed

+27
-30
lines changed

Lib/random.py

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939

4040
from warnings import warn as _warn
4141
from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil as _ceil
42-
from math import sqrt as _sqrt, acos as _acos, cos as _cos, sin as _sin, tau as TWOPI
42+
from math import sqrt as _sqrt, acos as _acos, cos as _cos, sin as _sin
43+
from math import tau as TWOPI, floor as _floor
4344
from os import urandom as _urandom
4445
from _collections_abc import Set as _Set, Sequence as _Sequence
4546
from itertools import accumulate as _accumulate, repeat as _repeat
@@ -234,7 +235,7 @@ def __reduce__(self):
234235

235236
## -------------------- integer methods -------------------
236237

237-
def randrange(self, start, stop=None, step=1, _int=int):
238+
def randrange(self, start, stop=None, step=1):
238239
"""Choose a random item from range(start, stop[, step]).
239240
240241
This fixes the problem with randint() which includes the
@@ -244,7 +245,7 @@ def randrange(self, start, stop=None, step=1, _int=int):
244245

245246
# This code is a bit messy to make it fast for the
246247
# common case while still doing adequate error checking.
247-
istart = _int(start)
248+
istart = int(start)
248249
if istart != start:
249250
raise ValueError("non-integer arg 1 for randrange()")
250251
if stop is None:
@@ -253,7 +254,7 @@ def randrange(self, start, stop=None, step=1, _int=int):
253254
raise ValueError("empty range for randrange()")
254255

255256
# stop argument supplied.
256-
istop = _int(stop)
257+
istop = int(stop)
257258
if istop != stop:
258259
raise ValueError("non-integer stop for randrange()")
259260
width = istop - istart
@@ -263,7 +264,7 @@ def randrange(self, start, stop=None, step=1, _int=int):
263264
raise ValueError("empty range for randrange() (%d, %d, %d)" % (istart, istop, width))
264265

265266
# Non-unit step argument supplied.
266-
istep = _int(step)
267+
istep = int(step)
267268
if istep != step:
268269
raise ValueError("non-integer step for randrange()")
269270
if istep > 0:
@@ -296,7 +297,7 @@ def _randbelow_with_getrandbits(self, n):
296297
r = getrandbits(k)
297298
return r
298299

299-
def _randbelow_without_getrandbits(self, n, int=int, maxsize=1<<BPF):
300+
def _randbelow_without_getrandbits(self, n, maxsize=1<<BPF):
300301
"""Return a random int in the range [0,n). Returns 0 if n==0.
301302
302303
The implementation does not use getrandbits, but only random.
@@ -307,15 +308,15 @@ def _randbelow_without_getrandbits(self, n, int=int, maxsize=1<<BPF):
307308
_warn("Underlying random() generator does not supply \n"
308309
"enough bits to choose from a population range this large.\n"
309310
"To remove the range limitation, add a getrandbits() method.")
310-
return int(random() * n)
311+
return _floor(random() * n)
311312
if n == 0:
312313
return 0
313314
rem = maxsize % n
314315
limit = (maxsize - rem) / maxsize # int(limit * maxsize) % n == 0
315316
r = random()
316317
while r >= limit:
317318
r = random()
318-
return int(r * maxsize) % n
319+
return _floor(r * maxsize) % n
319320

320321
_randbelow = _randbelow_with_getrandbits
321322

@@ -346,10 +347,10 @@ def shuffle(self, x, random=None):
346347
'since Python 3.9 and will be removed in a subsequent '
347348
'version.',
348349
DeprecationWarning, 2)
349-
_int = int
350+
floor = _floor
350351
for i in reversed(range(1, len(x))):
351352
# pick an element in x[:i+1] with which to exchange x[i]
352-
j = _int(random() * (i + 1))
353+
j = floor(random() * (i + 1))
353354
x[i], x[j] = x[j], x[i]
354355

355356
def sample(self, population, k, *, counts=None):
@@ -462,9 +463,9 @@ def choices(self, population, weights=None, *, cum_weights=None, k=1):
462463
n = len(population)
463464
if cum_weights is None:
464465
if weights is None:
465-
_int = int
466+
floor = _floor
466467
n += 0.0 # convert to float for a small speed improvement
467-
return [population[_int(random() * n)] for i in _repeat(None, k)]
468+
return [population[floor(random() * n)] for i in _repeat(None, k)]
468469
cum_weights = list(_accumulate(weights))
469470
elif weights is not None:
470471
raise TypeError('Cannot specify both weights and cumulative weights')
@@ -814,24 +815,20 @@ def _notimplemented(self, *args, **kwds):
814815
## -------------------- test program --------------------
815816

816817
def _test_generator(n, func, args):
817-
import time
818-
print(n, 'times', func.__name__)
819-
total = 0.0
820-
sqsum = 0.0
821-
smallest = 1e10
822-
largest = -1e10
823-
t0 = time.perf_counter()
824-
for i in range(n):
825-
x = func(*args)
826-
total += x
827-
sqsum = sqsum + x*x
828-
smallest = min(x, smallest)
829-
largest = max(x, largest)
830-
t1 = time.perf_counter()
831-
print(round(t1 - t0, 3), 'sec,', end=' ')
832-
avg = total / n
833-
stddev = _sqrt(sqsum / n - avg * avg)
834-
print('avg %g, stddev %g, min %g, max %g\n' % (avg, stddev, smallest, largest))
818+
from statistics import stdev, fmean as mean
819+
from time import perf_counter
820+
821+
t0 = perf_counter()
822+
data = [func(*args) for i in range(n)]
823+
t1 = perf_counter()
824+
825+
xbar = mean(data)
826+
sigma = stdev(data, xbar)
827+
low = min(data)
828+
high = max(data)
829+
830+
print(f'{t1 - t0:.3f} sec, {n} times {func.__name__}')
831+
print('avg %g, stddev %g, min %g, max %g\n' % (xbar, sigma, low, high))
835832

836833

837834
def _test(N=2000):

0 commit comments

Comments
 (0)