For that, I got the solution but I can't understand it: I have used below function to generate random numbers within the given range but what is the meaning of below function?
static int randomRangeInNumber(int min, int max) {
Random r=new Random();
return r.nextInt((max-min)+1)+min;
}
What i have returned i didnt get Please help making me understand its meaning any help will be appreciated
nextInt is normally exclusive of the top value, so add 1 to make it inclusive
So, in your case, we'll say the max
is 10 and min
is 5.
nextInt(n)
will give you a random number between 0 and n - 1.
So adding the +1
will let you include the value of n
.
nextInt(max-min)
therefore gives you a random number between (in this case) 0 and 5 - 1 (and include the 5 because of the +1)
Then adding the min
again, means it will give you the above random number, but add 5 to it, so the result will be a random number between 5 and 10.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments