srand((float)microtime()*1000000);
function shuffleDeck() {
global $deck;
global $targetcard;
global $decksize;
$deck = array();
for ($i = 0; $i < $decksize; $i++) {
if ($i < $targetcard) {
$deck[] = 1; // add a target card to the deck
}
else {
$deck[] = 0; // add a normal card to the deck
}
}
return;
}
function dealCard() {
global $deck;
global $decksize;
$card = array_splice($deck, array_rand($deck), 1);
return $card[0];
}
function printGames ($startingCard, $numlines) {
global $winners;
global $decksize;
global $sumWinners;
// if we've requested x lines, but less are left
if ($startingCard + $numlines > $decksize + 1) {
// print out however many we can
$numlines = $decksize - $startingCard + 1;
}
// rough error checking
if ($startingCard < 0 ||
$startingCard > $decksize ||
$numlines < 1) {
return;
}
if ($numlines == 1) {
$sumWinners += $winners[$startingCard];
echo "
\n";
echo "
".$startingCard."
\n";
echo "
".$winners[$startingCard]."
\n";
echo "
".$sumWinners."
\n";
echo "
\n";
return;
}
// print out multiple games at a time
$tempGames = 0;
for ($i = 0; $i < $numlines; $i++) {
$tempGames += $winners[$startingCard + $i];
}
$sumWinners += $tempGames;
echo "
\n";
return;
}
for ($g = 0; $g < $GAMES; $g++) {
shuffleDeck();
$numfound = 0;
$numdrawn = 0;
// while we haven't drawn enough cards yet and
// there are cards left in the deck, keep searching
while ($numdrawn++ < $numtodraw && count($deck) > 0) {
$card = dealCard();
switch($card) {
// 1 is a target card
case 1: $numfound++;
break;
// didn't find anything special
default: break;
} // switch
} // while
// how many target cards did I find?
$winners[$numfound]++;
} // for each game
// Calculate the average number of target cards found
$averageWinner = 0;
for ($i = 1; $i <= $decksize; $i++) {
$averageWinner += $winners[$i] * $i;
}
$averageWinner /= $GAMES;
?>
The average number of target tiles found in
draws is
0.