http://qs321.pair.com?node_id=145571

On average, how large a group is required for two people to have the same birthday? I remembered the answer was lower than I expected, but I forgot what the answer was.

Stuff like that bothers me. I suck at probability theory so often use Monte Carlo method for solutions.

So who cares? If you are in a room with this number of people with nothing to do (ie network is down), bet them all a buck that two people in the room share a birthday. Then send me a buck.

BuckFoo

#!/usr/bin/perl use strict; my $TRIALS = 1024; my $total; for (1..$TRIALS) { $total += trial(); } $total /= $TRIALS; print "$total\n"; sub trial { my (%days, $i); while (++$i) { if ($days{int(rand(365))}++) { return $i; } } }

Replies are listed 'Best First'.
Re: Birthday Chances
by atcroft (Abbot) on Feb 15, 2002 at 11:37 UTC
    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); }
    Update:

    thraxil is correct-I bow both to his math skill and to his remembering the correct answer. An article at http://www.people.virginia.edu/~rjh9u/birthday.html 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....

      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;


        Chris
Re: Birthday Chances
by ehdonhon (Curate) on Feb 15, 2002 at 23:17 UTC

    The question here is: What are the chances of any n people not having unique birthdays? That's a difficult question. But the cool thing about probability is that you can always find the probability of something by subtracting the probability of its inverse from one. So instead, ask: What are the chances of any n people having unique birthdays? That would be:

    (1/365) * ( 1/365 -1) * (1 /365 -2 ) * ... ( 1/365 - (n-1))

    And once you have that, the answer to the original question is:

    1 - ( (1/365) * ( 1/365 - 1 ) * ( 1/365 - 2) * ... ( 1/365 - (n-1)))

    Now just plug in different values of n to find the odds of n people not having unique birthdays.

      You might want to adjust your parenthization(is that a word?) ;)
      I think what you really want is:
      1 - ( 1/365 * 1/(365 - 1) * 1/(365 - 2) * ... * 1/(365 - (n-1)) )

      Kevin
Re: Birthday Chances
by belg4mit (Prior) on Feb 14, 2002 at 23:51 UTC
    You forgot leap years! Also, some description of the code/theory might be good.

    --
    perl -pe "s/\b;([st])/'\1/mg"

Re: Birthday Chances
by Anonymous Monk on Mar 05, 2002 at 19:29 UTC
    It is fun to run simulations of situations like that. I think it is most pleasing because all you have to do is write a little code, and you get a whole bunch of data in return.

    If you still care, here is the actual answer to that problem.

    P(n,d) is the probability that at least two people in a group of n will share the same birthday out of d possible birthdays.
    P(n,d) = 1 - d!/((d-n)!*d^n) (boy do we need MathML)
    Of course, this probability is always less than one since there is some chance nobody will share birthdays, so how many people you need in the room before you make your bet depends on how much you are willing to risk. If you want a 75% chance of winning the bet, you need at least 32 people. For a 99% chance, you only need 55 people.
Re: Birthday Chances
by Anonymous Monk on Mar 12, 2002 at 01:32 UTC
    A slightly different question is what is the number of people required to have a greater than 50% chance of two sharing the same birthday? I think the answer to this question is 22. Your question of "On average, how large a group is required for two people to have the same birthday?". Has a slightly anal answer of 367! That is the only way to guarantee two share the same birthday based on there being 366 days in the longest year (leap years must be included as people can be born on the 28th Feb) and an extra person to share the birthday.
Re: Birthday Chances
by tahnead (Initiate) on Jul 16, 2007 at 05:09 UTC
    what are the chances of this..... plz someone help!! a group of 30 co-workers 2 are born on 19 may, different years 2 are born on 31 july, different years 4 are born on 17 march, and of those 2 have the same years!

      Well if you just use the idea that choosing 2 people out of a group has a certain number of possibilities, it would stand to reason when the possibilities get larger than 366, you are probably going to have two people with the same birthday. If you choose two people out of a group of 28 people, there are 378 ways to do this without repeats! That makes the odds pretty good that two of them with have the same birthday.

        it would stand to reason when the possibilities get larger than 366, you are probably going to have two people with the same birthday

        Not probably but definitely.

        If you have a group of 366 (or more) people you always have 2 people that share the same birthday (assuming a non-leap year).

        This reasoning is called "pigeonhole principle".