Write a method like:
public static int getRandom(int from, int to) {
if (from < to)
return from + new Random().nextInt(Math.abs(to - from));
return from - new Random().nextInt(Math.abs(to - from));
}
It also takes into account the facts that the argument nextInt()must be positive, and from there can be more to .
source
share