Velocity Reviews > python & mathematical methods of picking numbers at random

# python & mathematical methods of picking numbers at random

Bart Nessux
Guest
Posts: n/a

 01-15-2004
I am using method 'a' below to pick 25 names from a pool of 225. A
co-worker is using method 'b' by running it 25 times and throwing out
the winning name (names are associated with numbers) after each run and
then re-counting the list and doing it all over again.

My boss thinks that 'b' is somehow less fair than 'a', but the only
thing I see wrong with it is that it is really inefficient and ugly as
it's doing manually what 'a' does automatically, other than that I think
the outcome of both methods (25 unique winners from a pool of 225) are
the same. Anyone disagree with that and if so, please demonstrate how
'b' isn't as fair as 'a'

count = len(list_of_potential_winners)

a = random.sample(range(count), 25)

b = random.sample(range(count), 1)

Thanks!
Bart

Jeff Epler
Guest
Posts: n/a

 01-15-2004
It sounds like your co-worker has re-written sample. random.sample(l, 1)
is the same as random.choice(l), so that's another source of inefficiency.

But why are *you* using
random.sample(range(len(x)), 25)
random.sample(x, 25)
?

Jeff

Bart Nessux
Guest
Posts: n/a

 01-15-2004
Jeff Epler wrote:
> It sounds like your co-worker has re-written sample. random.sample(l, 1)
> is the same as random.choice(l), so that's another source of inefficiency.
>
> But why are *you* using
> random.sample(range(len(x)), 25)
> random.sample(x, 25)
> ?
>
> Jeff
>

Because it works and it's fast and len(count) changes every drawing.

Paul Rubin
Guest
Posts: n/a

 01-15-2004
Bart Nessux <(E-Mail Removed)> writes:
> I am using method 'a' below to pick 25 names from a pool of 225. A
> co-worker is using method 'b' by running it 25 times and throwing out
> the winning name (names are associated with numbers) after each run
> and then re-counting the list and doing it all over again.
>
> My boss thinks that 'b' is somehow less fair than 'a',

Both are the same, as you can see by calculating the probability of
any given name being selected. What is the application, and the
computer environment? You may also need to worry about correlations
in the underlying Mersenne Twister PRNG. If the application is
something where randomness is very important (you're picking winners
for a big lottery or something) then you should use a better RNG.

Bart Nessux
Guest
Posts: n/a

 01-15-2004
Paul Rubin wrote:
> Bart Nessux <(E-Mail Removed)> writes:
>
>>I am using method 'a' below to pick 25 names from a pool of 225. A
>>co-worker is using method 'b' by running it 25 times and throwing out
>>the winning name (names are associated with numbers) after each run
>>and then re-counting the list and doing it all over again.
>>
>>My boss thinks that 'b' is somehow less fair than 'a',

>
>
> Both are the same, as you can see by calculating the probability of
> any given name being selected. What is the application, and the
> computer environment? You may also need to worry about correlations
> in the underlying Mersenne Twister PRNG. If the application is
> something where randomness is very important (you're picking winners
> for a big lottery or something) then you should use a better RNG.

We're raffling off crock-pots... that's why I think this is OK for our
purposes.

Terry Reedy
Guest
Posts: n/a

 01-16-2004

"Bart Nessux" <(E-Mail Removed)> wrote in message
news:bu6rvn\$njg\$(E-Mail Removed)...
> Jeff Epler wrote:
> > But why are *you* using
> > random.sample(range(len(x)), 25)
> > 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

Mark Borgerding
Guest
Posts: n/a

 01-16-2004
Bart Nessux wrote:
> Paul Rubin wrote:
>
>> Bart Nessux <(E-Mail Removed)> writes:
>>
>>> I am using method 'a' below to pick 25 names from a pool of 225. A
>>> co-worker is using method 'b' by running it 25 times and throwing out
>>> the winning name (names are associated with numbers) after each run
>>> and then re-counting the list and doing it all over again.
>>>
>>> My boss thinks that 'b' is somehow less fair than 'a',

>>
>>
>>
>> Both are the same, as you can see by calculating the probability of
>> any given name being selected. What is the application, and the
>> computer environment? You may also need to worry about correlations
>> in the underlying Mersenne Twister PRNG. If the application is
>> something where randomness is very important (you're picking winners
>> for a big lottery or something) then you should use a better RNG.

>
>
> We're raffling off crock-pots... that's why I think this is OK for our
> purposes.
>

Some will claim you cooked the numbers, even if it is a crock.
Let 'em blow off some steam, but don't chicken out. If you let them stew
for a day, they'll soften up and you'll eventually reach a cord.

Samuel Walters
Guest
Posts: n/a

 01-16-2004
| Bart Nessux said |

> I am using method 'a' below to pick 25 names from a pool of 225. A
> co-worker is using method 'b' by running it 25 times and throwing out the
> winning name (names are associated with numbers) after each run and then
> re-counting the list and doing it all over again.
>
> My boss thinks that 'b' is somehow less fair than 'a', but the only thing
> I see wrong with it is that it is really inefficient and ugly as it's
> doing manually what 'a' does automatically, other than that I think the
> outcome of both methods (25 unique winners from a pool of 225) are the
> same. Anyone disagree with that and if so, please demonstrate how 'b'
> isn't as fair as 'a'
>
> count = len(list_of_potential_winners)
>
> a = random.sample(range(count), 25)
>
> b = random.sample(range(count), 1)
>
> Thanks!
> Bart

I looked at the code for random.sample, and found out that the two methods
are probabilistically equivalent. Neither is more or less fair than the
other.

You can, however, poke fun at your cow-orker for using
random.sample(range(count, 1) when random.randint(1,count) would have done
the exact same thing with the way he used random.sample.

HTH

Sam Walters.

P.S. The code for sample in random.py is very simple and fairly
straightforward. You should take a peek at it. The basic algorithm is to
make a list of winners, choose a random number, then if the winner is not
retry until a new winner comes up. Repeat until you have the desired
number of winners.

--
Never forget the halloween documents.
http://www.opensource.org/halloween/
""" Where will Microsoft try to drag you today?
Do you really want to go there?"""

Bart Nessux
Guest
Posts: n/a

 01-16-2004
Terry Reedy wrote:
> "Bart Nessux" <(E-Mail Removed)> wrote in message
> news:bu6rvn\$njg\$(E-Mail Removed)...
>
>>Jeff Epler wrote:
>>
>>>But why are *you* using
>>> random.sample(range(len(x)), 25)
>>> 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)!

Ahh, I see what you mean.

Bart Nessux
Guest
Posts: n/a

 01-16-2004
Terry Reedy wrote:
> "Bart Nessux" <(E-Mail Removed)> wrote in message
> news:bu6rvn\$njg\$(E-Mail Removed)...
>
>>Jeff Epler wrote:
>>
>>>But why are *you* using
>>> random.sample(range(len(x)), 25)
>>> 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)
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/li...le-random.html

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post PerlFAQ Server Perl Misc 0 04-27-2011 04:00 AM PerlFAQ Server Perl Misc 0 02-12-2011 11:00 PM VK Javascript 15 05-02-2010 03:43 PM Alex Untitled Ruby 11 11-16-2009 09:45 AM globalrev Python 4 04-20-2008 08:12 AM