|
| 1 | +import java.util.*; |
1 | 2 |
|
| 3 | +public class Randomizer{ |
| 4 | + |
| 5 | + public static Random theInstance = null; |
| 6 | + |
| 7 | + public Randomizer(){ |
| 8 | + |
| 9 | + } |
| 10 | + |
| 11 | + public static Random getInstance(){ |
| 12 | + if(theInstance == null){ |
| 13 | + theInstance = new Random(); |
| 14 | + } |
| 15 | + return theInstance; |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * Return a random boolean value. |
| 20 | + * @return True or false value simulating a coin flip. |
| 21 | + */ |
| 22 | + public static boolean nextBoolean(){ |
| 23 | + return Randomizer.getInstance().nextBoolean(); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * This method simulates a weighted coin flip which will return |
| 28 | + * true with the probability passed as a parameter. |
| 29 | + * |
| 30 | + * @param probability The probability that the method returns true, |
| 31 | + * a value between 0 to 1 inclusive. |
| 32 | + * @return True or false value simulating a weighted coin flip. |
| 33 | + */ |
| 34 | + public static boolean nextBoolean(double probability){ |
| 35 | + return Randomizer.nextDouble() < probability; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * This method returns a random integer. |
| 40 | + * @return A random integer. |
| 41 | + */ |
| 42 | + public static int nextInt(){ |
| 43 | + return Randomizer.getInstance().nextInt(); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * This method returns a random integer between 0 and n, exclusive. |
| 48 | + * @param n The maximum value for the range. |
| 49 | + * @return A random integer between 0 and n, exclusive. |
| 50 | + */ |
| 51 | + public static int nextInt(int n){ |
| 52 | + return Randomizer.getInstance().nextInt(n); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Return a number between min and max, inclusive. |
| 57 | + * @param min The minimum integer value of the range, inclusive. |
| 58 | + * @param max The maximum integer value in the range, inclusive. |
| 59 | + * @return A random integer between min and max. |
| 60 | + */ |
| 61 | + public static int nextInt(int min, int max){ |
| 62 | + return min + Randomizer.nextInt(max - min + 1); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Return a random double between 0 and 1. |
| 67 | + * @return A random double between 0 and 1. |
| 68 | + */ |
| 69 | + public static double nextDouble(){ |
| 70 | + return Randomizer.getInstance().nextDouble(); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Return a random double between min and max. |
| 75 | + * @param min The minimum double value in the range. |
| 76 | + * @param max The maximum double value in the rang. |
| 77 | + * @return A random double between min and max. |
| 78 | + */ |
| 79 | + public static double nextDouble(double min, double max){ |
| 80 | + return min + (max - min) * Randomizer.nextDouble(); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Return a random color. |
| 85 | + * @return A random hex string that represents a color. |
| 86 | + */ |
| 87 | + public static String nextColor(){ |
| 88 | + String randomNum = Integer.toHexString(Randomizer.nextInt(0, 16777216)); |
| 89 | + return "'#" + randomNum + "'"; |
| 90 | + } |
| 91 | +} |
0 commit comments