1package com.osroyale.util;
3import java.util.Arrays;
5import java.util.Random;
6import java.util.concurrent.ThreadLocalRandom;
8import static com.google.common.base.Preconditions.checkArgument;
45* A
static-util
class that provides additional functionality for generating pseudo-random numbers. All functions in this
46 *
class are backed by {@link ThreadLocalRandom} rather than the more commonly used {@link Random}. It is generally preferred
47 * to use
this over {@code Random} because although {@code Random} is thread safe; the same seed is shared concurrently,
48 * which leads to contention between multiple threads and overhead as a result. Surprisingly because of the way that {@code
49 * ThreadLocalRandom} works, even in completely single-threaded situations it runs up to three times faster than {@code
52 * @author lare96 <http:
53 * @see <a href=
"http://java-performance.info/java-util-random-java-util-concurrent-threadlocalrandom-multithreaded-environments/"
54 * >java.util.Random and java.util.concurrent.ThreadLocalRandom in multithreaded environments</a>
56public final class RandomUtils {
61 private RandomUtils() {
69 public static boolean nextBoolean() {
70 return ThreadLocalRandom.current().nextBoolean();
78 public static double nextDouble() {
79 return ThreadLocalRandom.current().nextDouble();
90 public static float floatRandom(
float range) {
92 throw new IllegalArgumentException(
"range <= 0");
93 return ThreadLocalRandom.current().nextFloat() * range;
104 public static int inclusive(
int min,
int max) {
107 checkArgument(max >= min,
"max < min");
108 return ThreadLocalRandom.current().nextInt((max - min) + 1) + min;
121 public static double inclusive(
double min,
double max) {
124 checkArgument(max >= min,
"max < min");
125 return ThreadLocalRandom.current().nextDouble(max - min) + min;
135 public static int inclusive(
int range) {
136 return inclusive(0, range);
145 public static <T> T random(T[] array) {
146 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
155 public static int random(
int... array) {
156 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
165 public static long random(
long... array) {
166 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
175 public static double random(
double... array) {
176 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
185 public static short random(
short... array) {
186 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
195 public static byte random(
byte... array) {
196 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
205 public static float random(
float... array) {
206 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
215 public static boolean random(
boolean... array) {
216 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
225 public static char random(
char... array) {
226 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
235 public static <T> T random(List<T> list) {
236 if (list.isEmpty())
return null;
237 return list.get((
int) (ThreadLocalRandom.current().nextDouble() * list.size()));
246 public static <T> T[] shuffle(T[] array) {
247 for (
int i = array.length - 1; i > 0; i--) {
248 int index = ThreadLocalRandom.current().nextInt(i + 1);
250 array[index] = array[i];
262 public static int[] shuffle(
int[] array) {
263 for (
int i = array.length - 1; i > 0; i--) {
264 int index = ThreadLocalRandom.current().nextInt(i + 1);
265 int a = array[index];
266 array[index] = array[i];
278 public static long[] shuffle(
long[] array) {
279 for (
int i = array.length - 1; i > 0; i--) {
280 int index = ThreadLocalRandom.current().nextInt(i + 1);
281 long a = array[index];
282 array[index] = array[i];
294 public static double[] shuffle(
double[] array) {
295 for (
int i = array.length - 1; i > 0; i--) {
296 int index = ThreadLocalRandom.current().nextInt(i + 1);
297 double a = array[index];
298 array[index] = array[i];
310 public static short[] shuffle(
short[] array) {
311 for (
int i = array.length - 1; i > 0; i--) {
312 int index = ThreadLocalRandom.current().nextInt(i + 1);
313 short a = array[index];
314 array[index] = array[i];
326 public static byte[] shuffle(
byte[] array) {
327 for (
int i = array.length - 1; i > 0; i--) {
328 int index = ThreadLocalRandom.current().nextInt(i + 1);
329 byte a = array[index];
330 array[index] = array[i];
342 public static float[] shuffle(
float[] array) {
343 for (
int i = array.length - 1; i > 0; i--) {
344 int index = ThreadLocalRandom.current().nextInt(i + 1);
345 float a = array[index];
346 array[index] = array[i];
358 public static boolean[] shuffle(
boolean[] array) {
359 for (
int i = array.length - 1; i > 0; i--) {
360 int index = ThreadLocalRandom.current().nextInt(i + 1);
361 boolean a = array[index];
362 array[index] = array[i];
373 public static char[] shuffle(
char[] array) {
374 for (
int i = array.length - 1; i > 0; i--) {
375 int index = ThreadLocalRandom.current().nextInt(i + 1);
376 char a = array[index];
377 array[index] = array[i];
393 public static int inclusiveExcludes(
int min,
int max,
int... exclude) {
394 Arrays.sort(exclude);
396 int result = inclusive(min, max);
397 while (Arrays.binarySearch(exclude, result) >= 0) {
398 result = inclusive(min, max);
412 public static <T> T randomExclude(T[] array, T exclude) {
413 T result = random(array);
414 while (exclude.equals(result))
415 result = random(array);
426 public static boolean success(
double value) {
427 return value >= 1.00 || ThreadLocalRandom.current().nextDouble() <= value;