Class of 50 individuals, two people in the probability birthday is the same day? Java

package helloworld;
import java.util.Random;
public class hello {
        public static void main(String[] args) {
            for(int i = 0; i < n; ++i){
                boolean[] p = new boolean[365];
                for(int j = 0; j < 365; ++j) p[j] = false;
   
                for(int k = 0; k < 50; ++k){
                    Random r = new Random();
                    int x = r.nextInt(365);
                    if(p[x]) {
                        cnt++;
                        break;
                    }
                    else p[x] = true;
                }
            }
            System.out.println(cnt * 1.0 /n);
        } 
        static int cnt = 0;
        static int n = 10000;
}

 

Guess you like

Origin www.cnblogs.com/MasterYan576356467/p/11183794.html