Choose random integer in a range bigger than Integer.MAX_VALUE?
java, random
Solution
The problem you have is that `(max - min)` overflows and gives you a negative value.
You can use a `long` instead.
public int randomWithRange(int min, int max) {
return (int) ((m_random.nextLong() & Long.MAX_VALUE) % (1L + max - min)) + min;
}
Problem
I know there are another questions about random in a range but none of their answers accomplishes what I am trying to do. Actually they have the same error I have. I wrote this simple function to generate random with range. ``` Random m_random = new Random(); ... public int RandomWithRange(int min, int max) { return m_random.nextInt(max - min + 1) + min; } ``` If range is bigger than Integer.MAX_VALUE, it throws an IllegalArgumentException: n must be positive. I know it overflows and turn to a negative number. My question is how to handle that? Example ranges; - [0, Integer.MAX_VALUE] - [Integer.MIN_VALUE, Integer.MAX_VALUE] - [-100, Integer.MAX_VALUE] Note: min and max must be inclusive.