Birthday Paradox (Surprise)

Notation: P(x) means "the probability of x".

Given that you have N>=2 people in a group, the probability that at least one pair of them have the same birthday (i.e. the probability of collision) is 1-P(all have different birthdays).

Assuming there are 365 different birthdays, all equally likely, P(all have different birthdays) = (364/365)(363/365)...(365-N+1/365)

If you do the calculation (see the program below) you will find that if you have a group of only 23 people the probablility that all have different birthdays is less than 1/2 (approximately 0.492703). In other words, if you hash about 20 things into a table of size 365, you are more likely than not to have at least one collision!



#include <math.h>
#include <iostream>
#include <iomanip>

using namespace std ;

int main  ()
{
   int people ;
   double odds ;
  
   odds = 1.0 ;
   cout << "people    odds" << endl << endl ;

   cout.setf(ios::fixed) ;
   cout.setf(ios::right) ;

   for (people=1; people <= 365; people++)
   {
       odds = odds * (365-people+1)/365 ;
    
       cout << " " 
       << setprecision(0) << setw(4) << people << "    " 
       << setprecision(4)<< setw(9) << 100*odds << endl ;
   }

}