Skip to content

Instantly share code, notes, and snippets.

@OutOfBrain
Created August 3, 2015 19:55
Show Gist options
  • Select an option

  • Save OutOfBrain/579ab44506b57da65b51 to your computer and use it in GitHub Desktop.

Select an option

Save OutOfBrain/579ab44506b57da65b51 to your computer and use it in GitHub Desktop.
Calculate the birthday problem distribution
<?php
/**
* Calculate chances for the birthday problem.
*/
$number_rolls = 100;
// number of birthdays
for ($i = 2; $i <= 100; ++$i) {
$same_birthdays = 0;
for ($j = 0; $j < $number_rolls; ++$j) {
$numbers = [];
$counter = $i;
// distribute the birthdays
while ($counter--) {
$numbers[] = rand(1, 365);
}
// find the doubles
$doubles = array_filter(array_count_values($numbers), function($value) {
return $value != 1;
});
if (count($doubles)) {
++$same_birthdays;
}
}
print('For #' . $i . ' birthdays: ' . ($same_birthdays / $number_rolls * 100) . '%' . PHP_EOL);
}
For #2 birthdays: 0%
For #3 birthdays: 1%
For #4 birthdays: 2%
For #5 birthdays: 4%
For #6 birthdays: 6%
For #7 birthdays: 6%
For #8 birthdays: 4%
For #9 birthdays: 10%
For #10 birthdays: 6%
For #11 birthdays: 20%
For #12 birthdays: 10%
For #13 birthdays: 25%
For #14 birthdays: 24%
For #15 birthdays: 24%
For #16 birthdays: 27%
For #17 birthdays: 38%
For #18 birthdays: 34%
For #19 birthdays: 35%
For #20 birthdays: 49%
For #21 birthdays: 49%
For #22 birthdays: 47%
For #23 birthdays: 49%
For #24 birthdays: 55%
For #25 birthdays: 59%
For #26 birthdays: 60%
For #27 birthdays: 55%
For #28 birthdays: 65%
For #29 birthdays: 64%
For #30 birthdays: 74%
For #31 birthdays: 72%
For #32 birthdays: 77%
For #33 birthdays: 82%
For #34 birthdays: 80%
For #35 birthdays: 77%
For #36 birthdays: 83%
For #37 birthdays: 87%
For #38 birthdays: 83%
For #39 birthdays: 89%
For #40 birthdays: 93%
For #41 birthdays: 84%
For #42 birthdays: 92%
For #43 birthdays: 92%
For #44 birthdays: 99%
For #45 birthdays: 94%
For #46 birthdays: 91%
For #47 birthdays: 98%
For #48 birthdays: 95%
For #49 birthdays: 96%
For #50 birthdays: 98%
For #51 birthdays: 98%
For #52 birthdays: 97%
For #53 birthdays: 98%
For #54 birthdays: 98%
For #55 birthdays: 100%
For #56 birthdays: 99%
For #57 birthdays: 100%
For #58 birthdays: 100%
For #59 birthdays: 100%
For #60 birthdays: 99%
For #61 birthdays: 100%
For #62 birthdays: 99%
For #63 birthdays: 100%
For #64 birthdays: 100%
For #65 birthdays: 99%
For #66 birthdays: 100%
For #67 birthdays: 100%
For #68 birthdays: 100%
For #69 birthdays: 100%
For #70 birthdays: 100%
For #71 birthdays: 100%
For #72 birthdays: 100%
For #73 birthdays: 100%
For #74 birthdays: 100%
For #75 birthdays: 100%
For #76 birthdays: 100%
For #77 birthdays: 100%
For #78 birthdays: 100%
For #79 birthdays: 100%
For #80 birthdays: 100%
For #81 birthdays: 100%
For #82 birthdays: 100%
For #83 birthdays: 100%
For #84 birthdays: 100%
For #85 birthdays: 100%
For #86 birthdays: 100%
For #87 birthdays: 100%
For #88 birthdays: 100%
For #89 birthdays: 100%
For #90 birthdays: 100%
For #91 birthdays: 100%
For #92 birthdays: 100%
For #93 birthdays: 100%
For #94 birthdays: 100%
For #95 birthdays: 100%
For #96 birthdays: 100%
For #97 birthdays: 100%
For #98 birthdays: 100%
For #99 birthdays: 100%
For #100 birthdays: 100%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment