Created
September 24, 2020 11:38
-
-
Save Mr00Anderson/6c80b88191c7e12d66fba00902fe2730 to your computer and use it in GitHub Desktop.
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
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