How do I generate a random int
value in a specific range?
The following methods have bugs related to integer overflow:
randomNum = minimum + (int)(Math.random() * maximum);
// Bug: `randomNum` can be bigger than `maximum`.
Random rn = new Random();
int n = maximum  minimum + 1;
int i = rn.nextInt() % n;
randomNum = minimum + i;
// Bug: `randomNum` can be smaller than `minimum`.
2
Note that this approach is more biased and less efficient than a nextInt
approach, https://stackoverflow.com/a/738651/360211
One standard pattern for accomplishing this is:
Min + (int)(Math.random() * ((Max  Min) + 1))
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.
Math.random() * ( Max  Min )
This returns a value in the range [0,MaxMin)
, where ‘MaxMin’ is not included.
For example, if you want [5,10)
, you need to cover five integer values so you use
Math.random() * 5
This would return a value in the range [0,5)
, where 5 is not included.
Now you need to shift this range up to the range that you are targeting. You do this by adding the Min value.
Min + (Math.random() * (Max  Min))
You now will get a value in the range [Min,Max)
. Following our example, that means [5,10)
:
5 + (Math.random() * (10  5))
But, this 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:
Min + (int)(Math.random() * ((Max  Min) + 1))
And there you have it. A random integer value in the range [Min,Max]
, or per the example [5,10]
:
5 + (int)(Math.random() * ((10  5) + 1))
4
 87
The Sun documentation explicitly says that you should better use Random() if you need an int instead of Math.random() which produces a double.
Feb 23, 2012 at 23:26
 7
This is actually biased compared to nextInt methods stackoverflow.com/a/738651/360211
– westonDec 29, 2016 at 13:35
 8
“Biased” in this case means that after 2^53 executions, some numbers will have had one extra occourance, on average.
Aug 29, 2019 at 9:48
Even thought i use this too, i want to point out that this is not a true random number. Thats why it should not be used for any security functionality. But for any casual cases it is the most straight forwrd method.
– TobiasOct 20, 2020 at 13:28
Use:
Random ran = new Random();
int x = ran.nextInt(6) + 5;
The integer x
is now the random number that has a possible outcome of 510
.
0
When you need a lot of random numbers, I do not recommend the Random class in the API. It has just a too small period. Try the Mersenne twister instead. There is a Java implementation.
Dec 13, 2008 at 10:18
Before you post a new answer, consider there are already 65+ answers for this question. Please, make sure that your answer contributes information that is not among existing answers.
Feb 3, 2020 at 11:53
