Click to See Complete Forum and Search --> : Settle an argument (this is fun...)


aj_nsc
08-16-2007, 02:20 PM
Ok, I wrote a program to try and settle an argument, this is old school programming, and it could probably be written better, I just wrote something to get the job done and I was hoping you people could tell me if there is anything incorrect in the coding logic here.

The Argument:

If you simultaneously pick two cards out of a deck of 52, what are the chances of AT LEAST one of them being a club?

Guy 1 - 25%!!!
Guy 2 - 50% you moron!!!!

The Program:


$m = 0;

$iterations = 10000000;

for($i=0;$i<=$iterations;$i++) {

$range = 52;

@cards = ("1h","2h","3h","4h","5h","6h","7h","8h","9h","10h","Jh","Qh","Kh",
"1c","2c","3c","4c","5c","6c","7c","8c","9c","10c","Jc","Qc","Kc",
"1s","2s","3s","4s","5s","6s","7s","8s","9s","10s","Js","Qs","Ks",
"1d","2d","3d","4d","5d","6d","7d","8d","9d","10d","Jd","Qd","Kd",
);

$cardOne = $cards[int(rand($range))];
$cardTwo = $cards[int(rand($range))];

$bothCards = $cardOne.$cardTwo;

if ($bothCards =~ /c/) {
$m++;
}

}

print $m/($iterations/100)."\n";


It supposed to be 50%....but after 10 million iterations the program usually spits out between 43 and 45%....is this still correct or is there some little mistake in my code?

Thanks

LeeU
08-16-2007, 04:32 PM
I think it's because you're looking for exact and, because it's random, there is always a change it won't happen, making it less than 50%. (Just an opinion but makes sense to me.)

Yelgnidroc
08-17-2007, 07:00 AM
It's not 50%.

You need to consider 3 outcomes:

Probability that card one is a club and card two is not
Probability that card one is not a club and card two is
Probability that both are clubs.

Sorry, but it about 30 years since doing stats, so what to do with those 3 outcomes escapes me.

In your program, card 2 is selected from the same range as card 1, but card 2 can't be the same as card 1.

Yelgnidroc
08-17-2007, 09:02 AM
See http://www.stats.gla.ac.uk/steps/glossary/probability.html#addrule

Probability of 1st card being a club, and second card not = 13/52 = .25
Probability of 1st card being a nonclub, and second card club = 13/51 = .2549

Probability of both being clubs 13/52 * 12/51 = .05882

Total probaility = .25 + .2549 - .05882 = .446

I'm mega mega rusty so will stand to be corrected.

dragle
08-17-2007, 09:50 AM
Or the brute force way to look at it:

There are 2,652 possible two card combinations that could be drawn (52 possibilities for the first card times 51 possibilities for the second).

Of those, 1,482 are combinations where neither card is a club (39 possible cards for the first draw times 38 possible cards for the second draw). Therefore, 1,170 are combinations that do include at least one club (2,652 - 1,482 = 1,170).

Thus, the chances that at least one of the two cards drawn is a club are (1,170/2,652) * 100, or about 44.12%.

When I adjust your algorithm above to account for the fact that the second card cannot be the same as the first card (i.e., no replacement, as Yelgnidroc pointed out) and run the script for 1,000,000 iterations I get 44.0988, 44.1472, 44.1625, etc.

Cheers!

Yelgnidroc
08-17-2007, 10:13 AM
Good work dragle.

I wonder where my (ish) approach went wrong.

aj_nsc
08-17-2007, 10:22 AM
Yelgnidroc, you made a good point that I can't duplicate the cards drawn. I actually accounted for that this morning because I realized the same thing. I accounted for that.

As for the rest of you, thanks for the numbers. Mine comes out to 44 percent most of the time, too. Glad I have some math to back it up.