DEV Community

Theerachai Songsee
Theerachai Songsee

Posted on

Answer: How do I generate random integers within a specific range in Java?

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…

Top comments (0)