Languages
[Edit]
EN

Java - generate random int in range

7 points
Created by:
Tyreese-Aguirre
379

1. ThreadLocalRandom.current().nextInt(min, max);

Edit

Example:

2. new Random().nextInt((max - min) + 1) + min;

Edit

Example:

3. (int) (Math.random() * ((max - min) + 1)) + min;

Edit

Example:

4. new Random().ints(min, (max + 1)).limit(1).findFirst().getAsInt();

Edit

Example:

References:

Edit

ThreadLocalRandom.nextInt(int origin, int bound) - java docs
Random().nextInt(int bound) - java docs
Math.random() - java docs
Random().ints(int randomNumberOrigin, int randomNumberBound) - java docs

1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Java - random numbers

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join