A magic card drawing simulation with trials

0 && $deck[0] == 1) { array_splice($deck, 0, 1); return true; } else { return false; } } function dealCard() { global $deck; global $decksize; $card = array_splice($deck, array_rand($deck), 1); return $card[0]; } function printGames ($startingTurn, $numTurns) { global $winners; global $decksize; global $sumWinners; // if we've requested x turns, but less are left if ($startingTurn + $numTurns > count($winners)) { // print out however many we can $numTurns = count($winners) - $startingTurn; } // rough error checking if ($startingTurn < 0 || $startingTurn >= count($winners) || $numTurns < 1) { return; } if ($numTurns == 1) { $sumWinners += $winners[$startingTurn]; echo " \n"; echo " ".$startingTurn."\n"; echo " ".$winners[$startingTurn]."\n"; echo " ".$sumWinners."\n"; echo " \n"; return; } // print out multiple games at a time $tempGames = 0; for ($i = 0; $i < $numTurns; $i++) { $tempGames += $winners[$startingTurn + $i]; } $sumWinners += $tempGames; echo " \n"; echo " ".$startingTurn." - ".($startingTurn+$numTurns-1)."\n"; echo " ".$tempGames."\n"; echo " ".$sumWinners."\n"; echo " \n"; return; } function getStartingHand($handsize) { shuffleDeck(); $numfound = 0; $searchQueue = 0; // get starting hand for ($i = 0; $i < $handsize; $i++) { $card = dealCard(); switch($card) { // 1 is a target card case 1: $numfound++; break; // 2 is a search card case 2: $searchQueue++; break; default: break; } // switch } // resolve all searches now while ($searchQueue > 0) { if (removeTargetCard()) { $numfound++; } } return $numfound; } for ($g = 0; $g < $GAMES; $g++) { // Mulligan algorithm $numfound = getStartingHand(7); // mulligan to 6 if I have 0, 1, or 7 lands if ($numfound < 2 || $numfound > 6) { $numfound = getStartingHand(6); // mulligan to 5 if I have 0, 1, or 6 lands if ($numfound < 2 || $numfound > 5) { $numfound = getStartingHand(5); // mulligan to 4 if I have 0 lands if ($numfound < 1) { $numfound = getStartingHand(4); // mulligan to 3 if I have 0 lands if ($numfound < 1) { // keep 3 no matter what $numfound = getStartingHand(3); } } } } $turn = 0; // while we haven't found enough target cards and // there are cards left in the deck, keep searching while ($numfound < $numdesired && count($deck) > 0) { $turn++; $card = dealCard(); switch($card) { // 1 is a target card case 1: $numfound++; break; // 2 is a search card case 2: if (removeTargetCard()) { $numfound++; } break; // didn't find anything special default: break; } // switch } // while // if we went first, we actually only draw a card starting on turn 2 if ($turn > 0 && $gofirst) { $turn++; } // how many cards did I use? $winners[$turn]++; } // for each game // Calculate the average number of cards needed to hit // the desired number $averageWinner = 0; for ($i = 1; $i <= $decksize; $i++) { $averageWinner += $winners[$i] * $i; } $averageWinner /= $GAMES; ?> The average number of turns required to find copies of the target card is 0.

Turn Games Sum
Number of cards in deck:
Number of target cards:
Number of search cards:
Desired number of copies 
of the target card:
Go first?
Number of trials:
Number of lines to print together:

Mulligan algorithm:
Mulligan to 6 if 0, 1, or 7 target cards
Mulligan to 5 if 0, 1, or 6 target cards
Mulligan to 4 if 0 target cards
Mulligan to 3 if 0 target cards
Keep 3 no matter what


If you notice a problem with this page,
please e-mail me!