测试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输出:

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

Process finished with exit code 0

 

得出概率是几百万分之一

posted @ 2017-11-03 15:07  XIAOSHUA  阅读(504)  评论(0编辑  收藏  举报