Skip to content

Instantly share code, notes, and snippets.

@raimonizard
Created January 28, 2023 18:47
Show Gist options
  • Save raimonizard/eeaf0ea41b057aa7cae5bbef53c22388 to your computer and use it in GitHub Desktop.
Save raimonizard/eeaf0ea41b057aa7cae5bbef53c22388 to your computer and use it in GitHub Desktop.
Java method to generate an integer value inside a given interval
/**
* 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