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
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