RuneHive-Game
Loading...
Searching...
No Matches
RandomGen.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
8/**
9 * The {@link ThreadLocalRandom} wrapper that provides additional functionality
10 * for generating pseudo-random numbers. In order to avoid sharing instances of
11 * this class across multiple threads, this should only be instantiated locally
12 * unless certain that it will never be accessed by another thread.
13 * @author lare96 <http://github.com/lare96>
14 * @author Ryley Kimmel <ryley.kimmel@live.com>
15 */
16public final class RandomGen {
17
18 /**
19 * The backing {@link ThreadLocalRandom} that will pseudorandomly generate
20 * numbers. It is generally preferred to use this over {@link Random}
21 * because although {@code Random} is thread safe; the same seed is shared
22 * concurrently, which leads to contention between multiple threads and
23 * overhead as a result of that. Surprisingly because of the way that
24 * {@code ThreadLocalRandom} works, even in completely single-threaded
25 * situations it runs up to three times faster than {@code Random}.
26 * @see <a
27 * href="http://java-performance.info/java-util-random-java-util-concurrent-threadlocalrandom-multithreaded-environments/">java.util.Random
28 * and java.util.concurrent.ThreadLocalRandom in multithreaded
29 * environments</a>
30 */
31 private final ThreadLocalRandom random = ThreadLocalRandom.current();
32
33 /**
34 * Gets the backing {@link ThreadLocalRandom}.
35 * @return the backing random instance.
36 */
37 public ThreadLocalRandom get() {
38 return random;
39 }
40
41 /**
42 * Returns a pseudo-random {@code int} value between inclusive {@code min}
43 * and inclusive {@code max}.
44 * @param min the minimum inclusive number.
45 * @param max the maximum inclusive number.
46 * @return the pseudo-random {@code int}.
47 * @throws IllegalArgumentException if {@code max - min + 1} is less than {@code 0}.
48 */
49 public int inclusive(int min, int max) {
50 if(max < min) {
51 max = min + 1;
52 }
53 return random.nextInt((max - min) + 1) + min;
54 }
55
56 /**
57 * Returns a pseudo-random {@code int} value between inclusive {@code 0} and
58 * inclusive {@code range}.
59 * @param range the maximum inclusive number.
60 * @return the pseudo-random {@code int}.
61 * @throws IllegalArgumentException if {@code max - min + 1} is less than {@code 0}.
62 */
63 public int inclusive(int range) {
64 return inclusive(0, range);
65 }
66
67 /**
68 * Returns a pseudo-random {@code int} value between inclusive {@code min}
69 * and inclusive {@code max} excluding the specified numbers within the
70 * {@code excludes} array.
71 * @param min the minimum inclusive number.
72 * @param max the maximum inclusive number.
73 * @return the pseudo-random {@code int}.
74 * @throws IllegalArgumentException if {@code max - min + 1} is less than {@code 0}.
75 * @see {@link #inclusive(int, int)}.
76 */
77 public int inclusiveExcludes(int min, int max, int... exclude) {
78 Arrays.sort(exclude);
79
80 int result = inclusive(min, max);
81 while(Arrays.binarySearch(exclude, result) >= 0) {
82 result = inclusive(min, max);
83 }
84
85 return result;
86 }
87
88 /**
89 * Returns a pseudo-random {@code float} between inclusive {@code 0} and
90 * exclusive {@code range}.
91 * @param range The exclusive range.
92 * @return The pseudo-random {@code float}.
93 * @throws IllegalArgumentException If the specified range is less than {@code 0}.
94 */
95 public float floatRandom(float range) {
96 if(range < 0F)
97 throw new IllegalArgumentException("range <= 0");
98 return random.nextFloat() * range;
99 }
100
101 /**
102 * Pseudo-randomly retrieves an index from {@code array}.
103 * @param array the array to retrieve an index from.
104 * @return the element retrieved from the array.
105 */
106 public int randomIndex(Object[] array) {
107 return (int) (random.nextDouble() * array.length);
108 }
109
110 /**
111 * Pseudo-randomly retrieves a element from {@code array}.
112 * @param array the array to retrieve an element from.
113 * @return the element retrieved from the array.
114 */
115 public <T> T random(T[] array) {
116 return array[(int) (random.nextDouble() * array.length)];
117 }
118
119 /**
120 * Pseudo-randomly retrieves an {@code int} from this {@code array}.
121 * @param array the array to retrieve an {@code int} from.
122 * @return the {@code int} retrieved from the array.
123 */
124 public int random(int[] array) {
125 return array[(int) (random.nextDouble() * array.length)];
126 }
127
128 /**
129 * Pseudo-randomly retrieves an {@code long} from this {@code array}.
130 * @param array the array to retrieve an {@code long} from.
131 * @return the {@code long} retrieved from the array.
132 */
133 public long random(long[] array) {
134 return array[(int) (random.nextDouble() * array.length)];
135 }
136
137 /**
138 * Pseudo-randomly retrieves an {@code double} from this {@code array}.
139 * @param array the array to retrieve an {@code double} from.
140 * @return the {@code double} retrieved from the array.
141 */
142 public double random(double[] array) {
143 return array[(int) (random.nextDouble() * array.length)];
144 }
145
146 /**
147 * Pseudo-randomly retrieves an {@code short} from this {@code array}.
148 * @param array the array to retrieve an {@code short} from.
149 * @return the {@code short} retrieved from the array.
150 */
151 public short random(short[] array) {
152 return array[(int) (random.nextDouble() * array.length)];
153 }
154
155 /**
156 * Pseudo-randomly retrieves an {@code byte} from this {@code array}.
157 * @param array the array to retrieve an {@code byte} from.
158 * @return the {@code byte} retrieved from the array.
159 */
160 public byte random(byte[] array) {
161 return array[(int) (random.nextDouble() * array.length)];
162 }
163
164 /**
165 * Pseudo-randomly retrieves an {@code float} from this {@code array}.
166 * @param array the array to retrieve an {@code float} from.
167 * @return the {@code float} retrieved from the array.
168 */
169 public float random(float[] array) {
170 return array[(int) (random.nextDouble() * array.length)];
171 }
172
173 /**
174 * Pseudo-randomly retrieves an {@code boolean} from this {@code array}.
175 * @param array the array to retrieve an {@code boolean} from.
176 * @return the {@code boolean} retrieved from the array.
177 */
178 public boolean random(boolean[] array) {
179 return array[(int) (random.nextDouble() * array.length)];
180 }
181
182 /**
183 * Pseudo-randomly retrieves an {@code char} from this {@code array}.
184 * @param array the array to retrieve an {@code char} from.
185 * @return the {@code char} retrieved from the array.
186 */
187 public char random(char[] array) {
188 return array[(int) (random.nextDouble() * array.length)];
189 }
190
191 /**
192 * Pseudo-randomly retrieves a element from {@code list}.
193 * @param list the list to retrieve an element from.
194 * @return the element retrieved from the list.
195 */
196 public <T> T random(List<T> list) {
197 return list.get((int) (random.nextDouble() * list.size()));
198 }
199
200 /**
201 * An implementation of the Fisher-Yates shuffle algorithm that will shuffle
202 * the elements of an {@code T} array.
203 * @param array the array that will be shuffled.
204 * @return the shuffled array.
205 */
206 public <T> T[] shuffle(T[] array) {
207 for(int i = array.length - 1; i > 0; i--) {
208 int index = random.nextInt(i + 1);
209 T a = array[index];
210 array[index] = array[i];
211 array[i] = a;
212 }
213 return array;
214 }
215
216 /**
217 * An implementation of the Fisher-Yates shuffle algorithm that will shuffle
218 * the elements of an {@code int} array.
219 * @param array the array that will be shuffled.
220 * @return the shuffled array.
221 */
222 public int[] shuffle(int[] array) {
223 for(int i = array.length - 1; i > 0; i--) {
224 int index = random.nextInt(i + 1);
225 int a = array[index];
226 array[index] = array[i];
227 array[i] = a;
228 }
229 return array;
230 }
231
232 /**
233 * An implementation of the Fisher-Yates shuffle algorithm that will shuffle
234 * the elements of an {@code long} array.
235 * @param array the array that will be shuffled.
236 * @return the shuffled array.
237 */
238 public long[] shuffle(long[] array) {
239 for(int i = array.length - 1; i > 0; i--) {
240 int index = random.nextInt(i + 1);
241 long a = array[index];
242 array[index] = array[i];
243 array[i] = a;
244 }
245 return array;
246 }
247
248 /**
249 * An implementation of the Fisher-Yates shuffle algorithm that will shuffle
250 * the elements of an {@code double} array.
251 * @param array the array that will be shuffled.
252 * @return the shuffled array.
253 */
254 public double[] shuffle(double[] array) {
255 for(int i = array.length - 1; i > 0; i--) {
256 int index = random.nextInt(i + 1);
257 double a = array[index];
258 array[index] = array[i];
259 array[i] = a;
260 }
261 return array;
262 }
263
264 /**
265 * An implementation of the Fisher-Yates shuffle algorithm that will shuffle
266 * the elements of an {@code short} array.
267 * @param array the array that will be shuffled.
268 * @return the shuffled array.
269 */
270 public short[] shuffle(short[] array) {
271 for(int i = array.length - 1; i > 0; i--) {
272 int index = random.nextInt(i + 1);
273 short a = array[index];
274 array[index] = array[i];
275 array[i] = a;
276 }
277 return array;
278 }
279
280 /**
281 * An implementation of the Fisher-Yates shuffle algorithm that will shuffle
282 * the elements of an {@code byte} array.
283 * @param array the array that will be shuffled.
284 * @return the shuffled array.
285 */
286 public byte[] shuffle(byte[] array) {
287 for(int i = array.length - 1; i > 0; i--) {
288 int index = random.nextInt(i + 1);
289 byte a = array[index];
290 array[index] = array[i];
291 array[i] = a;
292 }
293 return array;
294 }
295
296 /**
297 * An implementation of the Fisher-Yates shuffle algorithm that will shuffle
298 * the elements of an {@code float} array.
299 * @param array the array that will be shuffled.
300 * @return the shuffled array.
301 */
302 public float[] shuffle(float[] array) {
303 for(int i = array.length - 1; i > 0; i--) {
304 int index = random.nextInt(i + 1);
305 float a = array[index];
306 array[index] = array[i];
307 array[i] = a;
308 }
309 return array;
310 }
311
312 /**
313 * An implementation of the Fisher-Yates shuffle algorithm that will shuffle
314 * the elements of an {@code boolean} array.
315 * @param array the array that will be shuffled.
316 * @return the shuffled array.
317 */
318 public boolean[] shuffle(boolean[] array) {
319 for(int i = array.length - 1; i > 0; i--) {
320 int index = random.nextInt(i + 1);
321 boolean a = array[index];
322 array[index] = array[i];
323 array[i] = a;
324 }
325 return array;
326 }
327
328 /**
329 * An implementation of the Fisher-Yates shuffle algorithm that will shuffle
330 * the elements of an {@code char} array.
331 * @param array the array that will be shuffled.
332 */
333 public char[] shuffle(char[] array) {
334 for(int i = array.length - 1; i > 0; i--) {
335 int index = random.nextInt(i + 1);
336 char a = array[index];
337 array[index] = array[i];
338 array[i] = a;
339 }
340 return array;
341 }
342
343 /**
344 * Determines if a pseudorandomly generated double rounded to two decimal
345 * places is below or equal to {@code value}.
346 * @param value the value to determine this for.
347 * @return {@code true} if successful, {@code false} otherwise.
348 */
349 public boolean success(double value) {
350 return random.nextDouble() <= value;
351 }
352}
The ThreadLocalRandom wrapper that provides additional functionality for generating pseudo-random num...
final ThreadLocalRandom random
The backing ThreadLocalRandom that will pseudorandomly generate numbers.
short random(short[] array)
Pseudo-randomly retrieves an short from this array.
char random(char[] array)
Pseudo-randomly retrieves an char from this array.
boolean[] shuffle(boolean[] array)
An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an boolean ...
short[] shuffle(short[] array)
An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an short ar...
public< T > T random(T[] array)
Pseudo-randomly retrieves a element from array.
float random(float[] array)
Pseudo-randomly retrieves an float from this array.
double random(double[] array)
Pseudo-randomly retrieves an double from this array.
int[] shuffle(int[] array)
An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an int arra...
int randomIndex(Object[] array)
Pseudo-randomly retrieves an index from array.
long[] shuffle(long[] array)
An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an long arr...
boolean random(boolean[] array)
Pseudo-randomly retrieves an boolean from this array.
char[] shuffle(char[] array)
An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an char arr...
long random(long[] array)
Pseudo-randomly retrieves an long from this array.
float[] shuffle(float[] array)
An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an float ar...
double[] shuffle(double[] array)
An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an double a...
int inclusiveExcludes(int min, int max, int... exclude)
Returns a pseudo-random int value between inclusive min and inclusive max excluding the specified num...
int inclusive(int min, int max)
Returns a pseudo-random int value between inclusive min and inclusive max.
byte[] shuffle(byte[] array)
An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an byte arr...
public< T > T random(List< T > list)
Pseudo-randomly retrieves a element from list.
int random(int[] array)
Pseudo-randomly retrieves an int from this array.
boolean success(double value)
Determines if a pseudorandomly generated double rounded to two decimal places is below or equal to va...
public< T > T[] shuffle(T[] array)
An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an T array.
float floatRandom(float range)
Returns a pseudo-random float between inclusive 0 and exclusive range.
byte random(byte[] array)
Pseudo-randomly retrieves an byte from this array.
int inclusive(int range)
Returns a pseudo-random int value between inclusive 0 and inclusive range.