RuneHive-Game
Loading...
Searching...
No Matches
RandomUtils.java
Go to the documentation of this file.
1package com.runehive.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
10/**
11 * A static-util class that provides additional functionality for generating pseudo-random numbers. All functions in this
12 * class are backed by {@link ThreadLocalRandom} rather than the more commonly used {@link Random}. It is generally preferred
13 * to use this over {@code Random} because although {@code Random} is thread safe; the same seed is shared concurrently,
14 * which leads to contention between multiple threads and overhead as a result. Surprisingly because of the way that {@code
15 * ThreadLocalRandom} works, even in completely single-threaded situations it runs up to three times faster than {@code
16 * Random}.
17 *
18 * @author lare96 <http://github.com/lare96>
19 * @see <a href= "http://java-performance.info/java-util-random-java-util-concurrent-threadlocalrandom-multithreaded-environments/"
20 * >java.util.Random and java.util.concurrent.ThreadLocalRandom in multithreaded environments</a>
21 */
22public final class RandomUtils {
23
24 /**
25 * A private constructor to discourage external instantiation.
26 */
27 private RandomUtils() {
28 }
29
30 /**
31 * Returns a pseudo-random {@code boolean}.
32 *
33 * @return The pseudo-random {@code boolean}.
34 */
35 public static boolean nextBoolean() {
36 return ThreadLocalRandom.current().nextBoolean();
37 }
38
39 /**
40 * Returns a pseudo-random {@code double}.
41 *
42 * @return The pseudo-random {@code double}.
43 */
44 public static double nextDouble() {
45 return ThreadLocalRandom.current().nextDouble();
46 }
47
48 /**
49 * Returns a pseudo-random {@code float} between inclusive {@code 0} and
50 * exclusive {@code range}.
51 *
52 * @param range The exclusive range.
53 * @return The pseudo-random {@code float}.
54 * @exception IllegalArgumentException If the specified range is less than {@code 0}.
55 */
56 public static float floatRandom(float range) {
57 if (range < 0F)
58 throw new IllegalArgumentException("range <= 0");
59 return ThreadLocalRandom.current().nextFloat() * range;
60 }
61
62 /**
63 * Returns a pseudo-random {@code int} value between inclusive {@code min} and inclusive {@code max}.
64 *
65 * @param min The minimum inclusive number.
66 * @param max The maximum inclusive number.
67 * @return The pseudo-random {@code int}.
68 * @exception IllegalArgumentException If {@code max - min + 1} is less than {@code 0}.
69 */
70 public static int inclusive(int min, int max) {
71 if (min == max)
72 return min;
73 checkArgument(max >= min, "max < min");
74 return ThreadLocalRandom.current().nextInt((max - min) + 1) + min;
75 }
76
77 /**
78 * Returns a pseudo-random {@code int} value between inclusive {@code min}
79 * and inclusive {@code max}.
80 *
81 * @param min The minimum inclusive number.
82 * @param max The maximum inclusive number.
83 * @return The pseudo-random {@code int}.
84 * @exception IllegalArgumentException If {@code max - min + 1} is less than
85 * {@code 0}.
86 */
87 public static double inclusive(double min, double max) {
88 if (min == max)
89 return min;
90 checkArgument(max >= min, "max < min");
91 return ThreadLocalRandom.current().nextDouble(max - min) + min;
92 }
93
94 /**
95 * Returns a pseudo-random {@code int} value between inclusive {@code 0} and inclusive {@code range}.
96 *
97 * @param range The maximum inclusive number.
98 * @return The pseudo-random {@code int}.
99 * @exception IllegalArgumentException If {@code max - min + 1} is less than {@code 0}.
100 */
101 public static int inclusive(int range) {
102 return inclusive(0, range);
103 }
104
105 /**
106 * Pseudo-randomly retrieves a element from {@code array}.
107 *
108 * @param array The array to retrieve an element from.
109 * @return The element retrieved from the array.
110 */
111 public static <T> T random(T[] array) {
112 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
113 }
114
115 /**
116 * Pseudo-randomly retrieves an {@code int} from this {@code array}.
117 *
118 * @param array The array to retrieve an {@code int} from.
119 * @return The {@code int} retrieved from the array.
120 */
121 public static int random(int... array) {
122 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
123 }
124
125 /**
126 * Pseudo-randomly retrieves an {@code long} from this {@code array}.
127 *
128 * @param array The array to retrieve an {@code long} from.
129 * @return The {@code long} retrieved from the array.
130 */
131 public static long random(long... array) {
132 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
133 }
134
135 /**
136 * Pseudo-randomly retrieves an {@code double} from this {@code array}.
137 *
138 * @param array The array to retrieve an {@code double} from.
139 * @return The {@code double} retrieved from the array.
140 */
141 public static double random(double... array) {
142 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
143 }
144
145 /**
146 * Pseudo-randomly retrieves an {@code short} from this {@code array}.
147 *
148 * @param array The array to retrieve an {@code short} from.
149 * @return The {@code short} retrieved from the array.
150 */
151 public static short random(short... array) {
152 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
153 }
154
155 /**
156 * Pseudo-randomly retrieves an {@code byte} from this {@code array}.
157 *
158 * @param array The array to retrieve an {@code byte} from.
159 * @return The {@code byte} retrieved from the array.
160 */
161 public static byte random(byte... array) {
162 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
163 }
164
165 /**
166 * Pseudo-randomly retrieves an {@code float} from this {@code array}.
167 *
168 * @param array The array to retrieve an {@code float} from.
169 * @return The {@code float} retrieved from the array.
170 */
171 public static float random(float... array) {
172 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
173 }
174
175 /**
176 * Pseudo-randomly retrieves an {@code boolean} from this {@code array}.
177 *
178 * @param array The array to retrieve an {@code boolean} from.
179 * @return The {@code boolean} retrieved from the array.
180 */
181 public static boolean random(boolean... array) {
182 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
183 }
184
185 /**
186 * Pseudo-randomly retrieves an {@code char} from this {@code array}.
187 *
188 * @param array The array to retrieve an {@code char} from.
189 * @return The {@code char} retrieved from the array.
190 */
191 public static char random(char... array) {
192 return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)];
193 }
194
195 /**
196 * Pseudo-randomly retrieves a element from {@code list}.
197 *
198 * @param list The list to retrieve an element from.
199 * @return The element retrieved from the list.
200 */
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()));
204 }
205
206 /**
207 * An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an {@code T} array.
208 *
209 * @param array The array that will be shuffled.
210 * @return The shuffled array.
211 */
212 public static <T> T[] shuffle(T[] array) {
213 for (int i = array.length - 1; i > 0; i--) {
214 int index = ThreadLocalRandom.current().nextInt(i + 1);
215 T a = array[index];
216 array[index] = array[i];
217 array[i] = a;
218 }
219 return array;
220 }
221
222 /**
223 * An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an {@code int} array.
224 *
225 * @param array The array that will be shuffled.
226 * @return The shuffled array.
227 */
228 public static int[] shuffle(int[] array) {
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];
233 array[i] = a;
234 }
235 return array;
236 }
237
238 /**
239 * An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an {@code long} array.
240 *
241 * @param array The array that will be shuffled.
242 * @return The shuffled array.
243 */
244 public static long[] shuffle(long[] array) {
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];
249 array[i] = a;
250 }
251 return array;
252 }
253
254 /**
255 * An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an {@code double} array.
256 *
257 * @param array The array that will be shuffled.
258 * @return The shuffled array.
259 */
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];
265 array[i] = a;
266 }
267 return array;
268 }
269
270 /**
271 * An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an {@code short} array.
272 *
273 * @param array The array that will be shuffled.
274 * @return The shuffled array.
275 */
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];
281 array[i] = a;
282 }
283 return array;
284 }
285
286 /**
287 * An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an {@code byte} array.
288 *
289 * @param array The array that will be shuffled.
290 * @return The shuffled array.
291 */
292 public static byte[] shuffle(byte[] array) {
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];
297 array[i] = a;
298 }
299 return array;
300 }
301
302 /**
303 * An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an {@code float} array.
304 *
305 * @param array The array that will be shuffled.
306 * @return The shuffled array.
307 */
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];
313 array[i] = a;
314 }
315 return array;
316 }
317
318 /**
319 * An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an {@code boolean} array.
320 *
321 * @param array The array that will be shuffled.
322 * @return The shuffled array.
323 */
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];
329 array[i] = a;
330 }
331 return array;
332 }
333
334 /**
335 * An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an {@code char} array.
336 *
337 * @param array The array that will be shuffled.
338 */
339 public static char[] shuffle(char[] array) {
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];
344 array[i] = a;
345 }
346 return array;
347 }
348
349 /**
350 * Returns a pseudo-random {@code int} value between inclusive {@code min}
351 * and inclusive {@code max} excluding the specified numbers within the
352 * {@code excludes} array.
353 *
354 * @param min the minimum inclusive number.
355 * @param max the maximum inclusive number.
356 * @return the pseudo-random {@code int}.
357 * @exception IllegalArgumentException if {@code max - min + 1} is less than {@code 0}.
358 */
359 public static int inclusiveExcludes(int min, int max, int... exclude) {
360 Arrays.sort(exclude);
361
362 int result = inclusive(min, max);
363 while (Arrays.binarySearch(exclude, result) >= 0) {
364 result = inclusive(min, max);
365 }
366
367 return result;
368 }
369
370 /**
371 * Returns a pseudo-random {@code int} value between inclusive {@code min}
372 * and inclusive {@code max} excluding the specified numbers within the
373 * {@code excludes} array.
374 *
375 * @return the pseudo-random {@code int}.
376 * @exception IllegalArgumentException if {@code max - min + 1} is less than {@code 0}.
377 */
378 public static <T> T randomExclude(T[] array, T exclude) {
379 T result = random(array);
380 while (exclude.equals(result))
381 result = random(array);
382 return result;
383 }
384
385 /**
386 * Determines if a pseudorandomly generated double rounded to two decimal
387 * places is below or equal to {@code value}.
388 *
389 * @param value the value to determine this for.
390 * @return {@code true} if successful, {@code false} otherwise.
391 */
392 public static boolean success(double value) {
393 return value >= 1.00 || ThreadLocalRandom.current().nextDouble() <= value;
394 }
395
396}
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.