Re: python & mathematical methods of picking numbers at random
From: Samuel Walters (swalters_usenet_at_yahoo.com)
Date: 01/16/04
- Next message: JanC: "Re: ProtoCiv: porting Freeciv to Python CANNED"
- Previous message: Tim Roberts: "Re: dde"
- In reply to: Bart Nessux: "python & mathematical methods of picking numbers at random"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 16 Jan 2004 04:47:09 GMT
| 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
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. http://www.opensource.org/halloween/ """ Where will Microsoft try to drag you today? Do you really want to go there?"""
- Next message: JanC: "Re: ProtoCiv: porting Freeciv to Python CANNED"
- Previous message: Tim Roberts: "Re: dde"
- In reply to: Bart Nessux: "python & mathematical methods of picking numbers at random"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|