Last active
June 1, 2016 17:03
-
-
Save chris-moreton/e8899359495cd9e534517c0577803198 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$packs = []; | |
$swaps = []; | |
const ATTEMPTS = 5000; | |
const CARDS_PER_PACK = 5; | |
const TOTAL_UNIQUE_STICKERS = 680; | |
const COST_PER_PACK = 50; | |
const COST_PER_MULTIPACK = 275; | |
const PACKS_PER_MULTIPACK = 6; | |
for ($attempt = 0; $attempt < ATTEMPTS; $attempt ++) { | |
$packs[$attempt] = 0; | |
$swaps[$attempt] = 0; | |
$duplicatesInSamePack[$attempt] = 0; | |
$stickers = array_fill(0, TOTAL_UNIQUE_STICKERS, false); | |
while (in_array(false, $stickers)) { | |
$packs[$attempt] ++; | |
$thisPack = []; | |
for ($j = 0; $j < CARDS_PER_PACK; $j ++) { | |
do { | |
$r = rand(0, TOTAL_UNIQUE_STICKERS-1); | |
if (isset($thisPack[$r])) { | |
$duplicatesInSamePack[$attempt] ++; | |
} | |
} while (isset($thisPack[$r])); | |
$thisPack[$r] = true; | |
if ($stickers[$r]) { | |
$swaps[$attempt] ++; | |
} else { | |
$stickers[$r] = true; | |
} | |
} | |
} | |
} | |
$averagePacksBought = (array_sum($packs) / ATTEMPTS); | |
$multiPacksRequired = floor($averagePacksBought / PACKS_PER_MULTIPACK) + ($averagePacksBought % PACKS_PER_MULTIPACK == 0 ? 0 : 1); | |
$costBuyingMultiPacks = ($multiPacksRequired * COST_PER_MULTIPACK); | |
$costBuyingSinglePacks = ($averagePacksBought * COST_PER_PACK); | |
echo "Average packs bought = " . number_format($averagePacksBought) . PHP_EOL; | |
echo "Average duplicates rejected per attempt = " . number_format(array_sum($duplicatesInSamePack) / ATTEMPTS) . PHP_EOL; | |
echo 'Cost (buying multipacks at ' . number_format(COST_PER_MULTIPACK / 100, 2) . ' for six packs) = ' . number_format($costBuyingMultiPacks / 100, 2) . PHP_EOL; | |
echo 'Cost (buying single packs at ' . COST_PER_PACK . 'p each) = ' . number_format($costBuyingSinglePacks / 100, 2) . PHP_EOL; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment