測試Random類nextInt()方法連續兩次結果同樣的機率

  public static void main(String[] args) {
        int count = 0;
        int a = 0;
        Random r = new Random();
        while (true) {
            int b = r.nextInt(10000000);
            if (a == b) {
                break;
            } else {
                a = b;
            }
            System.out.println(count++ + ": " + b);
        }
        System.out.println("相同了");
    }

Console輸出:dom

8085724: 6260107
8085725: 5648643
8085726: 3241799
8085727: 2604678
8085728: 3444480
相同了spa

Process finished with exit code 0code

 

得出機率是幾百萬分之一blog

相關文章
相關標籤/搜索