Birthday problem
For a start, we can show off how to get the exact solution. If we pick n people, the total number of possible arrangements of birthdays is 365<sup>n</sup>
. Among those possibilities, there are C<sup>n</sup><sub>365</sub>
where all birthdays are different. For each of these, there are n!
possible ways to arrange the n people. So the solution is 1 - n!C<sup>n</sup><sub>365</sub>/365<sup>n</sup>
, which in Perl 6 can be written:
say "$_ :", 1 - combinations(365, $_)/365**$_ * [*] 1..$_ for ^365
Output:
0 : 0
1 : 0
2 : 0.002740
3 : 0.0082042
4 : 0.016355912
5 : 0.027135573700
6 : 0.04046248364911
7 : 0.0562357030959754
8 : 0.0743352923516690285
9 : 0.0946238338891667
^C
Now comparing with a simulation :
sub theory($n) { 1 - combinations(365, $n)/365**$n* [*] 1..$n }
sub simulation(:number-of-people($n), :sample-size($N) = 1_000) {
$N R/ grep ?*, ((^365).roll($n).unique !== $n) xx $N;
}
for 2 .. 365 -> $n {
printf "%3d people, theory: %.4f, simulation: %.4f\n",
$n, theory($n), simulation(number-of-people => $n);
}
Output:
2 people, theory: 0.0027, simulation: 0.0020
3 people, theory: 0.0082, simulation: 0.0080
4 people, theory: 0.0164, simulation: 0.0130
5 people, theory: 0.0271, simulation: 0.0260
6 people, theory: 0.0405, simulation: 0.0340
7 people, theory: 0.0562, simulation: 0.0590
8 people, theory: 0.0743, simulation: 0.0730
9 people, theory: 0.0946, simulation: 0.1080
10 people, theory: 0.1169, simulation: 0.1120
11 people, theory: 0.1411, simulation: 0.1220
12 people, theory: 0.1670, simulation: 0.1740
13 people, theory: 0.1944, simulation: 0.2200
14 people, theory: 0.2231, simulation: 0.2290
15 people, theory: 0.2529, simulation: 0.2540
16 people, theory: 0.2836, simulation: 0.2820
17 people, theory: 0.3150, simulation: 0.3190
18 people, theory: 0.3469, simulation: 0.3740
19 people, theory: 0.3791, simulation: 0.3720
20 people, theory: 0.4114, simulation: 0.3810
21 people, theory: 0.4437, simulation: 0.4340
22 people, theory: 0.4757, simulation: 0.4700
23 people, theory: 0.5073, simulation: 0.4960
24 people, theory: 0.5383, simulation: 0.5200
25 people, theory: 0.5687, simulation: 0.5990
26 people, theory: 0.5982, simulation: 0.5980
27 people, theory: 0.6269, simulation: 0.6520
28 people, theory: 0.6545, simulation: 0.6430
29 people, theory: 0.6810, simulation: 0.6690
30 people, theory: 0.7063, simulation: 0.7190
31 people, theory: 0.7305, simulation: 0.7450
^C