[Python-bugs-list] [ python-Bugs-801342 ] Bug (documentation or real, your choice) in random.sample.

SourceForge.net noreply at sourceforge.net
Fri Sep 5 15:45:13 EDT 2003


Bugs item #801342, was opened at 2003-09-05 15:21
Message generated for change (Settings changed) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=801342&group_id=5470

Category: Python Library
Group: None
>Status: Closed
>Resolution: Fixed
Priority: 5
Submitted By: Jeremy Fincher (jemfinch)
Assigned to: Raymond Hettinger (rhettinger)
Summary: Bug (documentation or real, your choice) in random.sample.

Initial Comment:
First, the bug: 
 
>>> s 
Set([1, 2, 4, 5, 6, 34]) 
>>> random.sample(s, 1) 
Traceback (most recent call last): 
  File "<stdin>", line 1, in ? 
  File "/usr/lib/python2.3/random.py", line 266, in 
sample 
    result[i] = selected[j] = population[j] 
TypeError: unindexable object 
 
The interesting thing is this: 
 
>>> random.sample(s, 4) 
[6, 4, 2, 5] 
 
So it works when the sample size is sufficiently 
large, but not when it's small relative to the size of 
the population. 
 
Now, the documentation: 
 
>>> print random.sample.__doc__ 
Chooses k unique random elements from a 
population sequence. 
<snip> 
 
I think either the documentation needs to be 
changed to indicate that the sequence must be 
indexable, or the behavior when the sample is 
small relative to the population should be changed 
to allow non-indexable sequences. 

----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2003-09-05 16:45

Message:
Logged In: YES 
user_id=80475

This isn't a bug, documentation or otherwise.  The docs 

specified that the population argument be a sequence 

(meaning that it defines __len__ and is indexable).



However, I do see the usefulness of taking samples of sets, 

so I fixed-up the code to accept sets, dictionaries, files, and 

other iterables defining __len__() that aren't indexable.



See:  Lib/random.py 1.51.8.2

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=801342&group_id=5470



More information about the Python-bugs-list mailing list