[Python-checkins] python/nondist/sandbox/twister random.py,1.3,1.4
rhettinger@users.sourceforge.net
rhettinger@users.sourceforge.net
2002年12月18日 11:00:03 -0800
Update of /cvsroot/python/python/nondist/sandbox/twister
In directory sc8-pr-cvs1:/tmp/cvs-serv15485
Modified Files:
random.py
Log Message:
In case someone is relying on the random=random parameter for shuffle(),
restored the parameter. Removed it completely from the new sample()
method which was not previously published.
Index: random.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/twister/random.py,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** random.py 18 Dec 2002 18:25:58 -0000 1.3
--- random.py 18 Dec 2002 19:00:00 -0000 1.4
***************
*** 237,241 ****
return seq[int(self.random() * len(seq))]
! def shuffle(self, x, _randbelow=None):
"""x, random=random.random -> shuffle list x in place; return None.
--- 237,241 ----
return seq[int(self.random() * len(seq))]
! def shuffle(self, x, random=None):
"""x, random=random.random -> shuffle list x in place; return None.
***************
*** 249,254 ****
"""
! if _randbelow is None:
_randbelow = self._randbelow
for i in xrange(len(x)-1, 0, -1):
# pick an element in x[:i+1] with which to exchange x[i]
--- 249,256 ----
"""
! if random is None:
_randbelow = self._randbelow
+ else:
+ _randbelow = lambda n: int(random()*n)
for i in xrange(len(x)-1, 0, -1):
# pick an element in x[:i+1] with which to exchange x[i]
***************
*** 256,260 ****
x[i], x[j] = x[j], x[i]
! def sample(self, population, k, _randbelow=None):
"""Chooses k unique random elements from a population sequence.
--- 258,262 ----
x[i], x[j] = x[j], x[i]
! def sample(self, population, k):
"""Chooses k unique random elements from a population sequence.
***************
*** 272,278 ****
This is especially fast and space efficient for sampling from a
large population: sample(xrange(10000000), 60)
-
- Optional arg random is a 0-argument function returning a random
- float in [0.0, 1.0); by default, the standard random.random.
"""
--- 274,277 ----
***************
*** 294,299 ****
if not 0 <= k <= n:
raise ValueError, "sample larger than population"
! if _randbelow is None:
! _randbelow = self._randbelow
result = [None] * k
if n < 6 * k: # if n len list takes less space than a k len dict
--- 293,297 ----
if not 0 <= k <= n:
raise ValueError, "sample larger than population"
! _randbelow = self._randbelow
result = [None] * k
if n < 6 * k: # if n len list takes less space than a k len dict