diff options
Diffstat (limited to 'Lib/random.py')
-rw-r--r-- | Lib/random.py | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/Lib/random.py b/Lib/random.py index 91be5adf84..14ac5e5b09 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -41,7 +41,7 @@ from types import MethodType as _MethodType, BuiltinMethodType as _BuiltinMethod from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil as _ceil from math import sqrt as _sqrt, acos as _acos, cos as _cos, sin as _sin from os import urandom as _urandom -from collections import Set as _Set, Sequence as _Sequence +from collections.abc import Set as _Set, Sequence as _Sequence from hashlib import sha512 as _sha512 __all__ = ["Random","seed","random","uniform","randint","choice","sample", @@ -162,18 +162,17 @@ class Random(_random.Random): ## -------------------- integer methods ------------------- - def randrange(self, start, stop=None, step=1, int=int): + def randrange(self, start, stop=None, step=1, _int=int): """Choose a random item from range(start, stop[, step]). This fixes the problem with randint() which includes the endpoint; in Python this is usually not what you want. - Do not supply the 'int' argument. """ # This code is a bit messy to make it fast for the # common case while still doing adequate error checking. - istart = int(start) + istart = _int(start) if istart != start: raise ValueError("non-integer arg 1 for randrange()") if stop is None: @@ -182,7 +181,7 @@ class Random(_random.Random): raise ValueError("empty range for randrange()") # stop argument supplied. - istop = int(stop) + istop = _int(stop) if istop != stop: raise ValueError("non-integer stop for randrange()") width = istop - istart @@ -192,7 +191,7 @@ class Random(_random.Random): raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width)) # Non-unit step argument supplied. - istep = int(step) + istep = _int(step) if istep != step: raise ValueError("non-integer step for randrange()") if istep > 0: @@ -251,18 +250,26 @@ class Random(_random.Random): raise IndexError('Cannot choose from an empty sequence') return seq[i] - def shuffle(self, x, random=None, int=int): + def shuffle(self, x, random=None): """x, random=random.random -> shuffle list x in place; return None. Optional arg random is a 0-argument function returning a random float in [0.0, 1.0); by default, the standard random.random. + """ - randbelow = self._randbelow - for i in reversed(range(1, len(x))): - # pick an element in x[:i+1] with which to exchange x[i] - j = randbelow(i+1) if random is None else int(random() * (i+1)) - x[i], x[j] = x[j], x[i] + if random is None: + randbelow = self._randbelow + for i in reversed(range(1, len(x))): + # pick an element in x[:i+1] with which to exchange x[i] + j = randbelow(i+1) + x[i], x[j] = x[j], x[i] + else: + _int = int + for i in reversed(range(1, len(x))): + # pick an element in x[:i+1] with which to exchange x[i] + j = _int(random() * (i+1)) + x[i], x[j] = x[j], x[i] def sample(self, population, k): """Chooses k unique random elements from a population sequence or set. @@ -633,7 +640,7 @@ class SystemRandom(Random): return (int.from_bytes(_urandom(7), 'big') >> 3) * RECIP_BPF def getrandbits(self, k): - """getrandbits(k) -> x. Generates a long int with k random bits.""" + """getrandbits(k) -> x. Generates an int with k random bits.""" if k <= 0: raise ValueError('number of bits must be greater than zero') if k != int(k): |