Answer: How do I generate random integers within a specific range in Java?
Theerachai Songsee
Posted on February 7, 2020
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…
💖 💪 🙅 🚩
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.