Generate random number between 2 numbers

Generate random number between 2 numbers

The following snippet will help you generate a random number in Java that is between 2 numbers (min and max).

static Random random = new Random();
/**
 * Min is inclusive and max is exclusive in this case
 **/
public static int randomBetween(int min, int max) {
    return random.nextInt(max - min) + min;
}

Please note that the min variable is inclusive in the results of this method while max is exclusive.

Menu