[Python-checkins] python/dist/src/Lib random.py,1.47,1.48
rhettinger@users.sourceforge.net
rhettinger@users.sourceforge.net
Fri, 13 Jun 2003 00:01:57 -0700
Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1:/tmp/cvs-serv12476
Modified Files:
random.py
Log Message:
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.
Index: random.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/random.py,v
retrieving revision 1.47
retrieving revision 1.48
diff -C2 -d -r1.47 -r1.48
*** random.py 24 May 2003 17:26:02 -0000 1.47
--- random.py 13 Jun 2003 07:01:51 -0000 1.48
***************
*** 208,212 ****
x[i], x[j] = x[j], x[i]
! def sample(self, population, k, int=int):
"""Chooses k unique random elements from a population sequence.
--- 208,212 ----
x[i], x[j] = x[j], x[i]
! def sample(self, population, k):
"""Chooses k unique random elements from a population sequence.
***************
*** 241,249 ****
raise ValueError, "sample larger than population"
random = self.random
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))
result[i] = pool[j]
pool[j] = pool[n-i-1] # move non-selected item into vacancy
--- 241,250 ----
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))
result[i] = pool[j]
pool[j] = pool[n-i-1] # move non-selected item into vacancy
***************
*** 251,257 ****
selected = {}
for i in xrange(k):
! j = int(random() * n)
while j in selected:
! j = int(random() * n)
result[i] = selected[j] = population[j]
return result
--- 252,258 ----
selected = {}
for i in xrange(k):
! j = _int(random() * n)
while j in selected:
! j = _int(random() * n)
result[i] = selected[j] = population[j]
return result