Created
September 20, 2016 10:56
-
-
Save MidnightDesign/db394cf630b9210721a5e9341a815d87 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
// 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