Skip to content

Instantly share code, notes, and snippets.

@Mr00Anderson
Created September 24, 2020 11:38
Show Gist options
  • Save Mr00Anderson/6c80b88191c7e12d66fba00902fe2730 to your computer and use it in GitHub Desktop.
Save Mr00Anderson/6c80b88191c7e12d66fba00902fe2730 to your computer and use it in GitHub Desktop.
package com.virtual_hex.math;
import java.util.Random;
public class RandomUtils {
/**
* Returns a pseudo-random number between min and max, inclusive.
* The difference between min and max can be at most
* <code>Integer.MAX_VALUE - 1</code>.
*
* @param min Minimum value
* @param max Maximum value. Must be greater than min.
* @return Integer between min and max, inclusive.
* @see java.util.Random#nextInt(int)
*/
public static int randInt(Random r, int min, int max) {
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = r.nextInt(max - min) + min;
return randomNum;
}
/**
* Returns a pseudo-random number between min and max, inclusive.
* The difference between min and max can be at most
* <code>Integer.MAX_VALUE - 1</code>.
*
* @param min Minimum value
* @param max Maximum value. Must be greater than min.
* @return Integer between min and max, inclusive.
* @see java.util.Random#nextInt(int)
*/
public static int randIntInclusive(Random r, int min, int max) {
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = r.nextInt((max - min) + 1) + min;
return randomNum;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment