jeudi 9 novembre 2023

Birthday Paradox's probability coming out to be zero,also not understanding how to use simulations

I have been trying to create a program which calculates the probability of 2 people's birthday being the same, considering there are 23 people in the room

I have made logic but the probability always comes out to be zero,it should be above 0.5,this is my first computer science class so i am not too familiar with java and coding.

I saw some programs online with multiple simulations,but i was not able to understand it.

import java.util.Random;

public class Main {
    
    public static int rand_bd() {

        Random rand = new Random();
        int x = rand.nextInt(365);
        return x;

    }
    public static void main(String[] args) {
        System.out.println("Hello world!");
        

        int h = 0;
        double count = 0.0;
        int g = rand_bd();



            for(int i = 0;i  <= 23;i++){


         h = rand_bd();
         if(g == h)
             count++;

        }
        System.out.println(count);
        System.out.println(count/23.0);
        System.out.println(rand_bd());
 
    }
}



Aucun commentaire:

Enregistrer un commentaire