Java Data Type How to - Count chance of duplication for random number within a range








Question

We would like to know how to count chance of duplication for random number within a range.

Answer

import java.util.Random;
//from   www  .j a  v a  2s . c  o m
public class Main {
    public static void main(String[] args) {
        for (int loop = 1; loop <= 1000; loop++) {
            int random1 = new Random().nextInt(100);
            int random2 = new Random().nextInt(100);
            if (random1 == random2) {
              System.out.println("Duplicate: "+random1);
            }
        }
    }
}

The code above generates the following result.