Created
January 28, 2023 18:47
-
-
Save raimonizard/eeaf0ea41b057aa7cae5bbef53c22388 to your computer and use it in GitHub Desktop.
Java method to generate an integer value inside a given interval
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This method generates an integer value inside a given interval (included) | |
* Credits: https://www.baeldung.com/java-generating-random-numbers-in-range | |
* import java.util.Random should be imported | |
* | |
* @param min The minimum accepted value | |
* @param max The max accepted value | |
* @return int The random value which matches the provided interval | |
*/ | |
public static int getRandomNumberUsingNextInt(int min, int max) { | |
Random random = new Random(); | |
return random.nextInt((max + 1) - min) + min; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment