The java Math library function Math.random() generates a double value in the range [0,1).
Notice this range does not include the 1.In order to get a specific range of values first you need to multiply by the magnitude of the range of values you want covered.
This returns a value in the range [0,Max-Min).
For example if you want [5,10] you need cover 5 integer values so you use
This would return a value in the range [0,5)
Now you need to shift this range up to the range that you are targeting. You do this by adding the Min value.
You now will get a value in the range [Min,Max). Following our example, that means [5,10):
But, this is still doesn't include Max and you are getting a double value. In order to get the Max value included, you need to add 1 to your range parameter (Max - Min) and then truncate the decimal part by casting to an int. This is accomplished via:
And there you have it. A random integer value in the range [Min,Max], or per the example [5,10]: