Skip to content

Instantly share code, notes, and snippets.

@MidnightDesign
Created September 20, 2016 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MidnightDesign/db394cf630b9210721a5e9341a815d87 to your computer and use it in GitHub Desktop.
Save MidnightDesign/db394cf630b9210721a5e9341a815d87 to your computer and use it in GitHub Desktop.
<?php
// https://www.youtube.com/watch?v=zzKGnuvX6IQ
$dice = [
'333333' => [3, 3, 3, 3, 3, 3],
'004444' => [4, 4, 4, 4, 0, 0],
'111555' => [5, 5, 5, 1, 1, 1],
'222266' => [2, 2, 2, 2, 6, 6],
];
$winners = [];
for ($i = 0; $i < 1000000; $i++) {
$rolled = array_map(function (array $die):int {
return $die[mt_rand(0, count($die) - 1)];
}, $dice);
$roundWinners = array_keys($rolled, max($rolled));
$winners[] = count($roundWinners) > 1 ? 'tie' : reset($roundWinners);
}
$groups = array_reduce($winners, function (array $groups, $winner):array {
if (!isset($groups[$winner])) {
$groups[$winner] = 0;
}
$groups[$winner]++;
return $groups;
}, []);
asort($groups);
foreach ($groups as $die => $wins) {
echo $die . ': ' . $wins . "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment