randInt function

int randInt(
  1. int min,
  2. int max
)

This method generates and returns a random integer from min to max (inclusive).

Implementation

int randInt(int min, int max) {
  // If `min` and `max` are the same number, just return that number.
  if (min == max) {
    return min;
  }

  // If `min` is greater than `max`, swap their values.
  if (min > max) {
    int temp = min;
    min = max;
    max = temp;
  }

  // Find the random number in the range, and return it.
  return Random().nextInt((max + 1) - min) + min;
}