mirror of
https://github.com/python/cpython.git
synced 2025-09-18 14:40:43 +00:00
Minor clean-up of function parameters in random().
This commit is contained in:
commit
be74a3d721
1 changed files with 17 additions and 12 deletions
|
@ -165,18 +165,17 @@ class Random(_random.Random):
|
||||||
|
|
||||||
## -------------------- integer methods -------------------
|
## -------------------- 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]).
|
"""Choose a random item from range(start, stop[, step]).
|
||||||
|
|
||||||
This fixes the problem with randint() which includes the
|
This fixes the problem with randint() which includes the
|
||||||
endpoint; in Python this is usually not what you want.
|
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
|
# This code is a bit messy to make it fast for the
|
||||||
# common case while still doing adequate error checking.
|
# common case while still doing adequate error checking.
|
||||||
istart = int(start)
|
istart = _int(start)
|
||||||
if istart != start:
|
if istart != start:
|
||||||
raise ValueError("non-integer arg 1 for randrange()")
|
raise ValueError("non-integer arg 1 for randrange()")
|
||||||
if stop is None:
|
if stop is None:
|
||||||
|
@ -185,7 +184,7 @@ class Random(_random.Random):
|
||||||
raise ValueError("empty range for randrange()")
|
raise ValueError("empty range for randrange()")
|
||||||
|
|
||||||
# stop argument supplied.
|
# stop argument supplied.
|
||||||
istop = int(stop)
|
istop = _int(stop)
|
||||||
if istop != stop:
|
if istop != stop:
|
||||||
raise ValueError("non-integer stop for randrange()")
|
raise ValueError("non-integer stop for randrange()")
|
||||||
width = istop - istart
|
width = istop - istart
|
||||||
|
@ -195,7 +194,7 @@ class Random(_random.Random):
|
||||||
raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))
|
raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))
|
||||||
|
|
||||||
# Non-unit step argument supplied.
|
# Non-unit step argument supplied.
|
||||||
istep = int(step)
|
istep = _int(step)
|
||||||
if istep != step:
|
if istep != step:
|
||||||
raise ValueError("non-integer step for randrange()")
|
raise ValueError("non-integer step for randrange()")
|
||||||
if istep > 0:
|
if istep > 0:
|
||||||
|
@ -254,21 +253,27 @@ class Random(_random.Random):
|
||||||
raise IndexError('Cannot choose from an empty sequence')
|
raise IndexError('Cannot choose from an empty sequence')
|
||||||
return seq[i]
|
return seq[i]
|
||||||
|
|
||||||
def shuffle(self, x, random=None, int=int):
|
def shuffle(self, x, random=None):
|
||||||
"""Shuffle list x in place, and return None.
|
"""Shuffle list x in place, and return None.
|
||||||
|
|
||||||
Optional argument random is a 0-argument function returning a
|
Optional argument random is a 0-argument function returning a
|
||||||
random float in [0.0, 1.0); if it is the default None, the
|
random float in [0.0, 1.0); if it is the default None, the
|
||||||
standard random.random will be used.
|
standard random.random will be used.
|
||||||
|
|
||||||
Do not supply the 'int' argument.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
randbelow = self._randbelow
|
if random is None:
|
||||||
for i in reversed(range(1, len(x))):
|
randbelow = self._randbelow
|
||||||
# pick an element in x[:i+1] with which to exchange x[i]
|
for i in reversed(range(1, len(x))):
|
||||||
j = randbelow(i+1) if random is None else int(random() * (i+1))
|
# pick an element in x[:i+1] with which to exchange x[i]
|
||||||
x[i], x[j] = x[j], 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):
|
def sample(self, population, k):
|
||||||
"""Chooses k unique random elements from a population sequence or set.
|
"""Chooses k unique random elements from a population sequence or set.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue