1package com.runehive.util;
3import java.util.Arrays;
5import java.util.Random;
6import java.util.concurrent.ThreadLocalRandom;
8import static com.google.common.base.Preconditions.checkArgument;
36 return ThreadLocalRandom.current().nextBoolean();
45 return ThreadLocalRandom.current().nextDouble();
58 throw new IllegalArgumentException(
"range <= 0");
59 return ThreadLocalRandom.current().nextFloat() * range;
73 checkArgument(max >= min,
"max < min");
74 return ThreadLocalRandom.current().nextInt((max - min) + 1) + min;
87 public static double inclusive(
double min,
double max) {
90 checkArgument(max >= min,
"max < min");
91 return ThreadLocalRandom.current().nextDouble(max - min) + min;
112 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
122 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
131 public static long random(
long... array) {
132 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
141 public static double random(
double... array) {
142 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
151 public static short random(
short... array) {
152 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
161 public static byte random(
byte... array) {
162 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
171 public static float random(
float... array) {
172 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
181 public static boolean random(
boolean... array) {
182 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
191 public static char random(
char... array) {
192 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
201 public static <T> T
random(List<T> list) {
202 if (list.isEmpty())
return null;
203 return list.get((
int) (ThreadLocalRandom.current().nextDouble() * list.size()));
213 for (
int i = array.length - 1; i > 0; i--) {
214 int index = ThreadLocalRandom.current().nextInt(i + 1);
216 array[index] = array[i];
229 for (
int i = array.length - 1; i > 0; i--) {
230 int index = ThreadLocalRandom.current().nextInt(i + 1);
231 int a = array[index];
232 array[index] = array[i];
245 for (
int i = array.length - 1; i > 0; i--) {
246 int index = ThreadLocalRandom.current().nextInt(i + 1);
247 long a = array[index];
248 array[index] = array[i];
260 public static double[]
shuffle(
double[] array) {
261 for (
int i = array.length - 1; i > 0; i--) {
262 int index = ThreadLocalRandom.current().nextInt(i + 1);
263 double a = array[index];
264 array[index] = array[i];
276 public static short[]
shuffle(
short[] array) {
277 for (
int i = array.length - 1; i > 0; i--) {
278 int index = ThreadLocalRandom.current().nextInt(i + 1);
279 short a = array[index];
280 array[index] = array[i];
293 for (
int i = array.length - 1; i > 0; i--) {
294 int index = ThreadLocalRandom.current().nextInt(i + 1);
295 byte a = array[index];
296 array[index] = array[i];
308 public static float[]
shuffle(
float[] array) {
309 for (
int i = array.length - 1; i > 0; i--) {
310 int index = ThreadLocalRandom.current().nextInt(i + 1);
311 float a = array[index];
312 array[index] = array[i];
324 public static boolean[]
shuffle(
boolean[] array) {
325 for (
int i = array.length - 1; i > 0; i--) {
326 int index = ThreadLocalRandom.current().nextInt(i + 1);
327 boolean a = array[index];
328 array[index] = array[i];
340 for (
int i = array.length - 1; i > 0; i--) {
341 int index = ThreadLocalRandom.current().nextInt(i + 1);
342 char a = array[index];
343 array[index] = array[i];
360 Arrays.sort(exclude);
363 while (Arrays.binarySearch(exclude, result) >= 0) {
380 while (exclude.equals(result))
393 return value >= 1.00 || ThreadLocalRandom.current().nextDouble() <= value;
static float[] shuffle(float[] array)
An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an float ar...
static int[] shuffle(int[] array)
An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an int arra...
static double[] shuffle(double[] array)
An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an double a...
static< T > T random(List< T > list)
Pseudo-randomly retrieves a element from list.
static byte[] shuffle(byte[] array)
An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an byte arr...
static< T > T randomExclude(T[] array, T exclude)
Returns a pseudo-random int value between inclusive min and inclusive max excluding the specified num...
static boolean nextBoolean()
Returns a pseudo-random boolean.
static int inclusiveExcludes(int min, int max, int... exclude)
Returns a pseudo-random int value between inclusive min and inclusive max excluding the specified num...
static int random(int... array)
Pseudo-randomly retrieves an int from this array.
static char random(char... array)
Pseudo-randomly retrieves an char from this array.
static int inclusive(int min, int max)
Returns a pseudo-random int value between inclusive min and inclusive max.
static float random(float... array)
Pseudo-randomly retrieves an float from this array.
static long random(long... array)
Pseudo-randomly retrieves an long from this array.
static boolean[] shuffle(boolean[] array)
An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an boolean ...
static short[] shuffle(short[] array)
An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an short ar...
static double inclusive(double min, double max)
Returns a pseudo-random int value between inclusive min and inclusive max.
static boolean random(boolean... array)
Pseudo-randomly retrieves an boolean from this array.
static short random(short... array)
Pseudo-randomly retrieves an short from this array.
static long[] shuffle(long[] array)
An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an long arr...
static< T > T[] shuffle(T[] array)
An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an T array.
static double nextDouble()
Returns a pseudo-random double.
static char[] shuffle(char[] array)
An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an char arr...
RandomUtils()
A private constructor to discourage external instantiation.
static float floatRandom(float range)
Returns a pseudo-random float between inclusive 0 and exclusive range.
static boolean success(double value)
Determines if a pseudorandomly generated double rounded to two decimal places is below or equal to va...
static double random(double... array)
Pseudo-randomly retrieves an double from this array.
static byte random(byte... array)
Pseudo-randomly retrieves an byte from this array.
static int inclusive(int range)
Returns a pseudo-random int value between inclusive 0 and inclusive range.
static< T > T random(T[] array)
Pseudo-randomly retrieves a element from array.