summaryrefslogtreecommitdiffstats
path: root/Lib/random.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2003-06-13 07:01:51 (GMT)
committerRaymond Hettinger <python@rcn.com>2003-06-13 07:01:51 (GMT)
commitfdbe5223b7402ee34c4f0c06caa6faabd9e73e35 (patch)
tree756f6566aa043a60d4d778458af1bcd2e35c7d51 /Lib/random.py
parent43e559a15588dd1d8713734cd3d9476fde2e92b7 (diff)
downloadcpython-fdbe5223b7402ee34c4f0c06caa6faabd9e73e35.zip
cpython-fdbe5223b7402ee34c4f0c06caa6faabd9e73e35.tar.gz
cpython-fdbe5223b7402ee34c4f0c06caa6faabd9e73e35.tar.bz2
SF bug #753602: random.sample not properly documented
The docs were fine but the "int=int" in the function call was both ugly and confusing. Moved it inside the body of the function definition.
Diffstat (limited to 'Lib/random.py')
-rw-r--r--Lib/random.py9
1 files changed, 5 insertions, 4 deletions
diff --git a/Lib/random.py b/Lib/random.py
index 3c05086..defddbe 100644
--- a/Lib/random.py
+++ b/Lib/random.py
@@ -207,7 +207,7 @@ class Random(_random.Random):
j = int(random() * (i+1))
x[i], x[j] = x[j], x[i]
- def sample(self, population, k, int=int):
+ def sample(self, population, k):
"""Chooses k unique random elements from a population sequence.
Returns a new list containing elements from the population while
@@ -240,19 +240,20 @@ class Random(_random.Random):
if not 0 <= k <= n:
raise ValueError, "sample larger than population"
random = self.random
+ _int = int
result = [None] * k
if n < 6 * k: # if n len list takes less space than a k len dict
pool = list(population)
for i in xrange(k): # invariant: non-selected at [0,n-i)
- j = int(random() * (n-i))
+ j = _int(random() * (n-i))
result[i] = pool[j]
pool[j] = pool[n-i-1] # move non-selected item into vacancy
else:
selected = {}
for i in xrange(k):
- j = int(random() * n)
+ j = _int(random() * n)
while j in selected:
- j = int(random() * n)
+ j = _int(random() * n)
result[i] = selected[j] = population[j]
return result