HIDE NAV

Random int Within Range

The below command line program demonstrates the generation of a pseudo-random int within a specific range.
The value generated by this algorithm lies between the specified minimum (inclusive) and maximum (exclusive) with all numbers equally probable.
Note that the minimum and maximum do not need to be variables and can be entered as literal values as well.

Main.java


package ceccs;

import java.util.Random;

public class Main {
    public static void main(String[] args) {
        Random randy = new Random();
        int min = 10;
        int max = 20;
        int randValue = randy.nextInt(max - min);
        randValue = randValue + min;
    }
}