python & mathematical methods of picking numbers at random

Terry Reedy tjreedy at udel.edu
Thu Jan 15 19:00:28 EST 2004


"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





More information about the Python-list mailing list