RuneHive-Game
Loading...
Searching...
No Matches
com.runehive.util.RandomGen Class Reference

The ThreadLocalRandom wrapper that provides additional functionality for generating pseudo-random numbers. More...

Public Member Functions

float floatRandom (float range)
 Returns a pseudo-random float between inclusive 0 and exclusive range.
ThreadLocalRandom get ()
 Gets the backing ThreadLocalRandom.
int inclusive (int min, int max)
 Returns a pseudo-random int value between inclusive min and inclusive max.
int inclusive (int range)
 Returns a pseudo-random int value between inclusive 0 and inclusive range.
int inclusiveExcludes (int min, int max, int... exclude)
 Returns a pseudo-random int value between inclusive min and inclusive max excluding the specified numbers within the excludes array.
boolean random (boolean[] array)
 Pseudo-randomly retrieves an boolean from this array.
byte random (byte[] array)
 Pseudo-randomly retrieves an byte from this array.
char random (char[] array)
 Pseudo-randomly retrieves an char from this array.
double random (double[] array)
 Pseudo-randomly retrieves an double from this array.
float random (float[] array)
 Pseudo-randomly retrieves an float from this array.
int random (int[] array)
 Pseudo-randomly retrieves an int from this array.
long random (long[] array)
 Pseudo-randomly retrieves an long from this array.
short random (short[] array)
 Pseudo-randomly retrieves an short from this array.
int randomIndex (Object[] array)
 Pseudo-randomly retrieves an index from array.
boolean[] shuffle (boolean[] array)
 An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an boolean array.
byte[] shuffle (byte[] array)
 An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an byte array.
char[] shuffle (char[] array)
 An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an char array.
double[] shuffle (double[] array)
 An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an double array.
float[] shuffle (float[] array)
 An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an float array.
int[] shuffle (int[] array)
 An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an int array.
long[] shuffle (long[] array)
 An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an long array.
short[] shuffle (short[] array)
 An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an short array.
boolean success (double value)
 Determines if a pseudorandomly generated double rounded to two decimal places is below or equal to value.

Package Functions

public< T > T random (List< T > list)
 Pseudo-randomly retrieves a element from list.
public< T > T random (T[] array)
 Pseudo-randomly retrieves a element from array.
public< T > T[] shuffle (T[] array)
 An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an T array.

Private Attributes

final ThreadLocalRandom random = ThreadLocalRandom.current()
 The backing ThreadLocalRandom that will pseudorandomly generate numbers.

Detailed Description

The ThreadLocalRandom wrapper that provides additional functionality for generating pseudo-random numbers.

In order to avoid sharing instances of this class across multiple threads, this should only be instantiated locally unless certain that it will never be accessed by another thread.

Author
lare96 http://github.com/lare96
Ryley Kimmel ryley.nosp@m..kim.nosp@m.mel@l.nosp@m.ive..nosp@m.com

Definition at line 16 of file RandomGen.java.

Member Function Documentation

◆ floatRandom()

float com.runehive.util.RandomGen.floatRandom ( float range)

Returns a pseudo-random float between inclusive 0 and exclusive range.

Parameters
rangeThe exclusive range.
Returns
The pseudo-random float.
Exceptions
IllegalArgumentExceptionIf the specified range is less than 0.

Definition at line 95 of file RandomGen.java.

95 {
96 if(range < 0F)
97 throw new IllegalArgumentException("range <= 0");
98 return random.nextFloat() * range;
99 }

References random.

◆ get()

ThreadLocalRandom com.runehive.util.RandomGen.get ( )

Gets the backing ThreadLocalRandom.

Returns
the backing random instance.

Definition at line 37 of file RandomGen.java.

37 {
38 return random;
39 }

References random.

◆ inclusive() [1/2]

int com.runehive.util.RandomGen.inclusive ( int min,
int max )

Returns a pseudo-random int value between inclusive min and inclusive max.

Parameters
minthe minimum inclusive number.
maxthe maximum inclusive number.
Returns
the pseudo-random int.
Exceptions
IllegalArgumentExceptionif max - min + 1 is less than 0.

Definition at line 49 of file RandomGen.java.

49 {
50 if(max < min) {
51 max = min + 1;
52 }
53 return random.nextInt((max - min) + 1) + min;
54 }

References random.

Referenced by inclusive(), inclusiveExcludes(), and com.runehive.game.world.entity.mob.npc.drop.NpcDrop.toItem().

Here is the caller graph for this function:

◆ inclusive() [2/2]

int com.runehive.util.RandomGen.inclusive ( int range)

Returns a pseudo-random int value between inclusive 0 and inclusive range.

Parameters
rangethe maximum inclusive number.
Returns
the pseudo-random int.
Exceptions
IllegalArgumentExceptionif max - min + 1 is less than 0.

Definition at line 63 of file RandomGen.java.

63 {
64 return inclusive(0, range);
65 }

References inclusive().

Here is the call graph for this function:

◆ inclusiveExcludes()

int com.runehive.util.RandomGen.inclusiveExcludes ( int min,
int max,
int... exclude )

Returns a pseudo-random int value between inclusive min and inclusive max excluding the specified numbers within the excludes array.

Parameters
minthe minimum inclusive number.
maxthe maximum inclusive number.
Returns
the pseudo-random int.
Exceptions
IllegalArgumentExceptionif max - min + 1 is less than 0.
See also
inclusive(int, int).

Definition at line 77 of file RandomGen.java.

77 {
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 }

References inclusive().

Here is the call graph for this function:

◆ random() [1/10]

boolean com.runehive.util.RandomGen.random ( boolean[] array)

Pseudo-randomly retrieves an boolean from this array.

Parameters
arraythe array to retrieve an boolean from.
Returns
the boolean retrieved from the array.

Definition at line 178 of file RandomGen.java.

178 {
179 return array[(int) (random.nextDouble() * array.length)];
180 }

References random.

◆ random() [2/10]

byte com.runehive.util.RandomGen.random ( byte[] array)

Pseudo-randomly retrieves an byte from this array.

Parameters
arraythe array to retrieve an byte from.
Returns
the byte retrieved from the array.

Definition at line 160 of file RandomGen.java.

160 {
161 return array[(int) (random.nextDouble() * array.length)];
162 }

References random.

◆ random() [3/10]

char com.runehive.util.RandomGen.random ( char[] array)

Pseudo-randomly retrieves an char from this array.

Parameters
arraythe array to retrieve an char from.
Returns
the char retrieved from the array.

Definition at line 187 of file RandomGen.java.

187 {
188 return array[(int) (random.nextDouble() * array.length)];
189 }

References random.

◆ random() [4/10]

double com.runehive.util.RandomGen.random ( double[] array)

Pseudo-randomly retrieves an double from this array.

Parameters
arraythe array to retrieve an double from.
Returns
the double retrieved from the array.

Definition at line 142 of file RandomGen.java.

142 {
143 return array[(int) (random.nextDouble() * array.length)];
144 }

References random.

◆ random() [5/10]

float com.runehive.util.RandomGen.random ( float[] array)

Pseudo-randomly retrieves an float from this array.

Parameters
arraythe array to retrieve an float from.
Returns
the float retrieved from the array.

Definition at line 169 of file RandomGen.java.

169 {
170 return array[(int) (random.nextDouble() * array.length)];
171 }

References random.

◆ random() [6/10]

int com.runehive.util.RandomGen.random ( int[] array)

Pseudo-randomly retrieves an int from this array.

Parameters
arraythe array to retrieve an int from.
Returns
the int retrieved from the array.

Definition at line 124 of file RandomGen.java.

124 {
125 return array[(int) (random.nextDouble() * array.length)];
126 }

References random.

◆ random() [7/10]

public< T > T com.runehive.util.RandomGen.random ( List< T > list)
package

Pseudo-randomly retrieves a element from list.

Parameters
listthe list to retrieve an element from.
Returns
the element retrieved from the list.

Definition at line 196 of file RandomGen.java.

196 {
197 return list.get((int) (random.nextDouble() * list.size()));
198 }

References random.

◆ random() [8/10]

long com.runehive.util.RandomGen.random ( long[] array)

Pseudo-randomly retrieves an long from this array.

Parameters
arraythe array to retrieve an long from.
Returns
the long retrieved from the array.

Definition at line 133 of file RandomGen.java.

133 {
134 return array[(int) (random.nextDouble() * array.length)];
135 }

References random.

◆ random() [9/10]

short com.runehive.util.RandomGen.random ( short[] array)

Pseudo-randomly retrieves an short from this array.

Parameters
arraythe array to retrieve an short from.
Returns
the short retrieved from the array.

Definition at line 151 of file RandomGen.java.

151 {
152 return array[(int) (random.nextDouble() * array.length)];
153 }

References random.

◆ random() [10/10]

public< T > T com.runehive.util.RandomGen.random ( T[] array)
package

Pseudo-randomly retrieves a element from array.

Parameters
arraythe array to retrieve an element from.
Returns
the element retrieved from the array.

Definition at line 115 of file RandomGen.java.

115 {
116 return array[(int) (random.nextDouble() * array.length)];
117 }

References random.

◆ randomIndex()

int com.runehive.util.RandomGen.randomIndex ( Object[] array)

Pseudo-randomly retrieves an index from array.

Parameters
arraythe array to retrieve an index from.
Returns
the element retrieved from the array.

Definition at line 106 of file RandomGen.java.

106 {
107 return (int) (random.nextDouble() * array.length);
108 }

References random.

◆ shuffle() [1/9]

boolean[] com.runehive.util.RandomGen.shuffle ( boolean[] array)

An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an boolean array.

Parameters
arraythe array that will be shuffled.
Returns
the shuffled array.

Definition at line 318 of file RandomGen.java.

318 {
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 }
val index

References random.

◆ shuffle() [2/9]

byte[] com.runehive.util.RandomGen.shuffle ( byte[] array)

An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an byte array.

Parameters
arraythe array that will be shuffled.
Returns
the shuffled array.

Definition at line 286 of file RandomGen.java.

286 {
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 }

References random.

◆ shuffle() [3/9]

char[] com.runehive.util.RandomGen.shuffle ( char[] array)

An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an char array.

Parameters
arraythe array that will be shuffled.

Definition at line 333 of file RandomGen.java.

333 {
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 }

References random.

◆ shuffle() [4/9]

double[] com.runehive.util.RandomGen.shuffle ( double[] array)

An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an double array.

Parameters
arraythe array that will be shuffled.
Returns
the shuffled array.

Definition at line 254 of file RandomGen.java.

254 {
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 }

References random.

◆ shuffle() [5/9]

float[] com.runehive.util.RandomGen.shuffle ( float[] array)

An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an float array.

Parameters
arraythe array that will be shuffled.
Returns
the shuffled array.

Definition at line 302 of file RandomGen.java.

302 {
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 }

References random.

◆ shuffle() [6/9]

int[] com.runehive.util.RandomGen.shuffle ( int[] array)

An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an int array.

Parameters
arraythe array that will be shuffled.
Returns
the shuffled array.

Definition at line 222 of file RandomGen.java.

222 {
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 }

References random.

◆ shuffle() [7/9]

long[] com.runehive.util.RandomGen.shuffle ( long[] array)

An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an long array.

Parameters
arraythe array that will be shuffled.
Returns
the shuffled array.

Definition at line 238 of file RandomGen.java.

238 {
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 }

References random.

◆ shuffle() [8/9]

short[] com.runehive.util.RandomGen.shuffle ( short[] array)

An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an short array.

Parameters
arraythe array that will be shuffled.
Returns
the shuffled array.

Definition at line 270 of file RandomGen.java.

270 {
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 }

References random.

◆ shuffle() [9/9]

public< T > T[] com.runehive.util.RandomGen.shuffle ( T[] array)
package

An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an T array.

Parameters
arraythe array that will be shuffled.
Returns
the shuffled array.

Definition at line 206 of file RandomGen.java.

206 {
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 }

References random.

◆ success()

boolean com.runehive.util.RandomGen.success ( double value)

Determines if a pseudorandomly generated double rounded to two decimal places is below or equal to value.

Parameters
valuethe value to determine this for.
Returns
true if successful, false otherwise.

Definition at line 349 of file RandomGen.java.

349 {
350 return random.nextDouble() <= value;
351 }

References random.

Member Data Documentation

◆ random

final ThreadLocalRandom com.runehive.util.RandomGen.random = ThreadLocalRandom.current()
private

The backing ThreadLocalRandom that will pseudorandomly generate numbers.

It is generally preferred to use this over Random because although Random is thread safe; the same seed is shared concurrently, which leads to contention between multiple threads and overhead as a result of that. Surprisingly because of the way that ThreadLocalRandom works, even in completely single-threaded situations it runs up to three times faster than Random.

See also
java.util.Random and java.util.concurrent.ThreadLocalRandom in multithreaded environments

Definition at line 31 of file RandomGen.java.

Referenced by floatRandom(), get(), inclusive(), random(), random(), random(), random(), random(), random(), random(), random(), random(), random(), randomIndex(), shuffle(), shuffle(), shuffle(), shuffle(), shuffle(), shuffle(), shuffle(), shuffle(), shuffle(), and success().


The documentation for this class was generated from the following file: