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
得出概率是几百万分之一