...that 5 of Andrew's 212 friends share a birthday.

#!/usr/bin/perl

#monte carlo calculation on Andrew's Birthday question.

# In short, this code allows a general test of the question:

# "if I have x friends, what are the chances that any y of

# them share a birthday?" It does so using Monte Carlo technique.

# runs should be set high (at least 1000, maybe higher) to give a

# good answer. I don't know what gives good confidence here.

# init a few things. bdaymatches is how many friends share one birthday.

use constant friends => 212;

use constant runs => 10000;

use constant bdaymatches => 5;

$foundmatches = 0;

# this loop calls yeartally once per run, adding each run to the array of

# runs called fullrun.

for ($i = 1; $i <= runs; $i++)

{

@temp = &yeartally;

$fullrun[$i] = [ @temp ];

#print "@temp\n";

# printf "THIS IS A YEAR BREAK \n";

}

#debug printing of the array:

# for $aref ( @fullrun) { print "\t [ @$aref ], \n"; }

for ($j=1; $j <= runs ; $j++ ) {

for ($k=0; $k <= 365 ; $k++) {

#print "$j, $k, $fullrun[$j][$k]";

if ($fullrun[$j][$k] >= bdaymatches) {

# print "incrementing...";

$foundmatches++;

}

}

}

#we really want matches/year

$foundmatches = $foundmatches / runs;

print "approx p for this formula: $foundmatches\n";

print "end of run";

# returns an array of counts of the number of people sharing each

# day of the year as a birthday.

sub yeartally()

#this is a sub because I thought that would save me zeroing things. Nope.

{

my @oneyear = ();

for ($j = 1; $j <= friends; $j++)

{

$x = int(365*rand);

$oneyear[$x]++;

# printf "%d \n", $x;

}

# print "@oneyear\n";

return @oneyear;

}

## Comments

## why you are writing a simulation...

when you can calculate the probability directly?

http://en.wikipedia.org/wiki/Birthday_problem

## Because it's tricky...

...it's not the normal case of the Birthday Problem.

In all the well-known "generalizations" of the Birthday Problem, you'll see they only want the odds of a single collision. The question of checking for an n-way collision is much much fussier.

I mean, I assume a reasonably probability-minded person could calculate the answer, but I challenge you to try. My preliminary sketch of an answer revealed a messy hairball: it is not trivially generalizable from a 2-way collision.

Given Andrew's 212 friends, his birthday p~=1. It's something like 10e-17 that he wouldn't match at least one pair. But given that a 5-way match is what we want, you then have to calculate the probable number of 2-way matches, then the odds that any of those 2-way matches are a 3-way match, and so forth. It's a very different problem from any version of the Birthday Problem, and I didn't remember enough combinatorial mathematics to figure out the odds.

I did remember enough Perl to write a program, though!

## I love programming

It's one of those things I'll never get tired of. Just knowing that I can understand the basic logic of the code makes me excited. I can't believe I actually read the full code of this post, heheh.

Hope you're well, Ryan. Give my best to TLO too.