I am trying to generate a random
intvalue with Java, but in a specific range.
My range is 5-10, meaning that 5 is the smallest possible value and 10 is the biggest. Any other number in between these numbers is possible to be a value, too.
In Java, there is a method
random() in the
Math class, which returns a
double value between 0.0 and 1.0. In the class
Random there is the method
nextInt(int n), which returns a random
int value in the range of 0 (inclusive) and n (exclusive). I couldn't find a method, which returns a random integer value between two numbers.
I have tried the following things, but I still have problems: (minimum and maximum are the smallest and biggest numbers).
randomNum = minimum + (int)(Math.random() * maximum);
randomNum can be bigger than
Random rn = new Random(); int n = maximum - minimum + 1; int i = rn.nextInt() % n; randomNum = minimum + i;
randomNum can be smaller than
How do I solve these problems?
I have tried also browsing through the archive and found:
But I couldn't solve the problem.