python & mathematical methods of picking numbers at random

Samuel Walters swalters_usenet at
Fri Jan 16 05:47:09 CET 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

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.


Sam Walters.

P.S.  The code for sample in 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
already in the list, add them.  If the winner is already in the list,
retry until a new winner comes up.  Repeat until you have the desired
number of winners.

Never forget the halloween documents.
""" Where will Microsoft try to drag you today?
    Do you really want to go there?"""

More information about the Python-list mailing list