RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
RandomUtils.java
1package com.osroyale.util;
2
3import java.util.Arrays;
4import java.util.List;
5import java.util.Random;
6import java.util.concurrent.ThreadLocalRandom;
7
8import static com.google.common.base.Preconditions.checkArgument;
9
44
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
50 * Random}.
51 *
52 * @author lare96 <http://github.com/lare96>
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>
55 */
56public final class RandomUtils {
57
61 private RandomUtils() {
62 }
63
69 public static boolean nextBoolean() {
70 return ThreadLocalRandom.current().nextBoolean();
71 }
72
78 public static double nextDouble() {
79 return ThreadLocalRandom.current().nextDouble();
80 }
81
90 public static float floatRandom(float range) {
91 if (range < 0F)
92 throw new IllegalArgumentException("range <= 0");
93 return ThreadLocalRandom.current().nextFloat() * range;
94 }
95
104 public static int inclusive(int min, int max) {
105 if (min == max)
106 return min;
107 checkArgument(max >= min, "max < min");
108 return ThreadLocalRandom.current().nextInt((max - min) + 1) + min;
109 }
110
121 public static double inclusive(double min, double max) {
122 if (min == max)
123 return min;
124 checkArgument(max >= min, "max < min");
125 return ThreadLocalRandom.current().nextDouble(max - min) + min;
126 }
127
135 public static int inclusive(int range) {
136 return inclusive(0, range);
137 }
138
145 public static <T> T random(T[] array) {
146 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
147 }
148
155 public static int random(int... array) {
156 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
157 }
158
165 public static long random(long... array) {
166 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
167 }
168
175 public static double random(double... array) {
176 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
177 }
178
185 public static short random(short... array) {
186 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
187 }
188
195 public static byte random(byte... array) {
196 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
197 }
198
205 public static float random(float... array) {
206 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
207 }
208
215 public static boolean random(boolean... array) {
216 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
217 }
218
225 public static char random(char... array) {
226 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
227 }
228
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()));
238 }
239
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);
249 T a = array[index];
250 array[index] = array[i];
251 array[i] = a;
252 }
253 return array;
254 }
255
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];
267 array[i] = a;
268 }
269 return array;
270 }
271
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];
283 array[i] = a;
284 }
285 return array;
286 }
287
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];
299 array[i] = a;
300 }
301 return array;
302 }
303
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];
315 array[i] = a;
316 }
317 return array;
318 }
319
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];
331 array[i] = a;
332 }
333 return array;
334 }
335
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];
347 array[i] = a;
348 }
349 return array;
350 }
351
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];
363 array[i] = a;
364 }
365 return array;
366 }
367
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];
378 array[i] = a;
379 }
380 return array;
381 }
382
393 public static int inclusiveExcludes(int min, int max, int... exclude) {
394 Arrays.sort(exclude);
395
396 int result = inclusive(min, max);
397 while (Arrays.binarySearch(exclude, result) >= 0) {
398 result = inclusive(min, max);
399 }
400
401 return result;
402 }
403
412 public static <T> T randomExclude(T[] array, T exclude) {
413 T result = random(array);
414 while (exclude.equals(result))
415 result = random(array);
416 return result;
417 }
418
426 public static boolean success(double value) {
427 return value >= 1.00 || ThreadLocalRandom.current().nextDouble() <= value;
428 }
429
430}