A better approach is:
int x = rand.nextInt(max - min + 1) + min;
Your formula generates numbers between min
and min + max
.
Random random = new Random(1234567);
int min = 5;
int max = 20;
while (true) {
int x = (int)(Math.random() * max) + min;
System.out.println(x);
if (x < min || x >= max) { break; }
}
Result:
10
16
13
21 // Oops!!
See it online here: ideone