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

ikungolf

Theerachai Songsee

Posted on February 7, 2020

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…

💖 💪 🙅 🚩
ikungolf
Theerachai Songsee

Posted on February 7, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related