python & mathematical methods of picking numbers at random

Bart Nessux bart_nessux at hotmail.com
Fri Jan 16 08:38:04 EST 2004


Terry Reedy wrote:
> "Bart Nessux" <bart_nessux at hotmail.com> wrote in message
> news:bu6rvn$njg$1 at solaris.cc.vt.edu...
> 
>>Jeff Epler wrote:
>>
>>>But why are *you* using
>>>    random.sample(range(len(x)), 25)
>>>instead of
>>>    random.sample(x, 25)
>>>?
> 
> 
>>Because it works and it's fast and len(count) changes every drawing.
> 
> 
> I think you missed Jeff's point, which is that you are repeating part of
> the work that sample tries to do for you.   From the Lib Ref:
> "
> sample(sequence, k): Return a k length list of unique elements chosen from
> the population sequence. Used for random sampling without replacement. New
> in version 2.3.
> 
> Returns a new list containing elements from the population while leaving
> the original population unchanged. The resulting list is in selection order
> so that all sub-slices will also be valid random samples. This allows
> raffle winners (the sample) to be partitioned into grand prize and second
> place winners (the subslices).
> "
> When you get the sample from range(n), you have to use them as indexes into
> x to get the actual list of names.  But the indexing and extraction is what
> sample would do if you gave it x instead of range(x)!
> 
> Terry J. Reedy
> 
> 

Also, the below statement should be removed from random's 
documentation... it's where I got the idea to do:

random.sample(range(len(x)), 25)
instead of
random.sample(x, 25)

"To choose a sample from a range of integers, use xrange
as an argument. This is especially fast and space efficient
for sampling from a large population: sample(xrange(10000000), 60)."

http://www.python.org/doc/current/lib/module-random.html




More information about the Python-list mailing list