lundi 8 février 2016

Java birthday paradox algorithm

I've made a small implementation of the famous birthday paradox, trying to find a collision between two random birthdates (here integer between 1 and 365) for the first time. But it returns always a value around let's say 40 and 70, which does not fit the stats at all. Is something wrong with my algo, or with the random int generator, both ? Thanks for your feedback.

Here is the code :

public static void main(String[] args){

int[] birthday = new int[200];

for(int i = 0; i<20;i++){
    Collision(birthday); 
}

}

public static int Collision(int birthday[]){

  Random rand = new Random();

  for(int i = 1; i<birthday.length;i++){

    birthday[i] = rand.nextInt(365);
  }

    int count = 0;

    for(int i = 0; i<birthday.length; i++){

      for(int j= i+1 ; j<birthday.length; j++){

        if (birthday[i] == birthday[j]){ 

          count++;}

      }

    }

    System.out.print(count+" ");

    return count;

  }

Here is the output for ex : 45 50 60 52 53 53 50 49 37 68 52 53 51 43 49 51 46 43 45 35




Aucun commentaire:

Enregistrer un commentaire