Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Birthday Chances

by atcroft (Abbot)
on Feb 15, 2002 at 11:37 UTC ( #145664=note: print w/replies, xml ) Need Help??

in reply to Birthday Chances

I saw this same problem a while back myself. If I recall correctly, the answer was 50 before the odds would be better than 50-50 of 2 people in the room having the same birthday. The 1st person as 1/365th of a chance of being on a particular day, the odds the 2nd person having one other than the first is 364/365ths, the 3rd having a different birthday 363/365ths, and so forth.

As to modeling this situation, you would have to keep track of the day (potentially an array 0..364), and add people until you hit the same day for two of them. Then, you would need to simulate it multiple times (such as your $TRIALS=1024) to get an estimate.

Enjoy. (The code below might give you a starting place, but may require you to run considerably longer to get a better estimate.)

#!/usr/bin/perl -w use strict; use warnings; srand(); my $maxday = 365; my $TRIALS = 1024000; my $average = 0; my $sum = 0; my $tenpercent = $TRIALS / 10; for (my $count = 0; $count < $TRIALS; $count++) { $sum += &test_bdays($maxday); if (($count) and (!($count % $tenpercent))) { $average = ($sum * 1.0) / $count; print("After $count trials, ", "the average number of people in the room was $average\n"); } } $average = ($sum * 1.0) / $TRIALS; print("After $TRIALS trials, ", " the average number of people in the room was $average\n"); sub test_bdays { my ($mday) = @_; my @days = (); my $d = $mday; my $people = 0; while ($d) { $days[$d] = 0; $d--; } my $flag = 0; while (!($flag)) { $people++; my $index = int(rand() * $mday); $days[$index]++; $flag = ($days[$index] > 1); } return($people); }

thraxil is correct-I bow both to his math skill and to his remembering the correct answer. An article at discusses it more. It had made me wonder when the simulations I ran with the script above seemed to fall within the range between 20 and 25....

Replies are listed 'Best First'.
Re: Re: Birthday Chances
by thraxil (Prior) on Feb 15, 2002 at 15:38 UTC

    you're close on the math.

    (ignoring leap-years) the probability that n people all have different birthdays is: ((365-1)/365) * ((365-2)/365) * ((365-3)/365) * . . . * ((365-n+1)/365) = 365! / ((365-n)! * 365^n)

    setting that equal to 50% and solving is probably harder than just plugging in numbers on a calculator till you get it. 23 is the magic number.

    anders pearson

      To expand on what thraxil has said...

      This probability and permutations exercise could be written as:

      #!/usr/bin/perl -w use strict; my $DAYS_IN_YEAR = 365; print "\n# of People \t Birthday Match Likelihood\n" . '-'x80 . "\n"; for my $people ( 2 .. 40 ){ my $tmp_days = $DAYS_IN_YEAR; my $probability=1; for ( 1 .. $people ){ $probability *= $tmp_days--; } $probability /= $DAYS_IN_YEAR ** $people; $probability = ( 1 - $probability ) * 100; print "$people \t\t $probability %\n"; } exit;


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://145664]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2022-08-10 17:38 GMT
Find Nodes?
    Voting Booth?

    No recent polls found