[Python-checkins] python/dist/src/Lib random.py,1.54,1.55

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Fri Sep 5 22:25:56 EDT 2003


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1:/tmp/cvs-serv4664

Modified Files:
	random.py 
Log Message:
SF bug #801342:  Bug (documentation or real, your choice) in random.sample.

random.sample() uses one of two algorithms depending on the ratio of the
sample size to the population size.  One of the algorithms accepted any
iterable population argument so long as it defined __len__().  The other
had a stronger requirement that the population argument be indexable.

While it met the documentation specifications which insisted that the
population argument be a sequence, it made random.sample() less usable
with sets.  So, the second algorithm was modified to coerce non-indexable
iterables and dictionaries into a tuple before proceeding.



Index: random.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/random.py,v
retrieving revision 1.54
retrieving revision 1.55
diff -C2 -d -r1.54 -r1.55
*** random.py	30 Aug 2003 01:24:19 -0000	1.54
--- random.py	6 Sep 2003 04:25:54 -0000	1.55
***************
*** 259,262 ****
--- 259,266 ----
                  pool[j] = pool[n-i-1]   # move non-selected item into vacancy
          else:
+             try:
+                 n > 0 and (population[0], population[n//2], population[n-1])
+             except (TypeError, KeyError):   # handle sets and dictionaries
+                 population = tuple(population)
              selected = {}
              for i in xrange(k):





More information about the Python-checkins mailing list