Package org.moeaframework.core
Class PRNG
java.lang.Object
org.moeaframework.core.PRNG
Static methods for generating random or pseudo-random numbers. Any source of randomness implementing the
Random
interface can be used as the random source.
PRNG
relies on an underlying source of randomness, and inherits thread safety from the underlying
implementation. Unless the underlying implementation is known to be thread-safe, assume that PRNG
is not
thread-safe.
-
Method Summary
Modifier and TypeMethodDescriptionstatic Random
Returns the source of randomness currently used.static boolean
Returns the next random, uniformly distributedboolean
value.static double
Returns the next random, uniformly distributeddouble
value between 0.0 and 1.0.static double
nextDouble
(double min, double max) Returns the next random, uniformly distributeddouble
value betweenmin
andmax
.static float
Returns the next random, uniformly distributedfloat
value between 0.0 and 1.0.static float
nextFloat
(float min, float max) Returns the next random, uniformly distributedfloat
value betweenmin
andmax
.static double
Returns the next random, Gaussian distributeddouble
value with mean0.0
and standard deviation1.0
.static double
nextGaussian
(double mean, double stdev) Returns the next random, Gaussian distributeddouble
value with meanmean
and standard deviationstdev
.static int
nextInt()
Returns the next random, uniformly distributedint
value betweenInteger.MIN_VALUE
andInteger.MAX_VALUE
.static int
nextInt
(int n) Returns the next random, uniformly distributedint
value between0
(inclusive) andn
(exclusive).static int
nextInt
(int min, int max) Returns the next random, uniformly distributedint
value betweenmin
andmax
(both inclusive).static <T> T
Returns a randomly selected item from the specified list.static <T> T
Returns a randomly selected item from the specified set.static void
Sets the source of randomness to be used.static void
setSeed
(long seed) While the preferred method for seeding PRNGs is through thesetRandom
method since methods providing more entropy may be available to specific implementations, this method is intended to provide a uniform interface for setting the seed.static void
shuffle
(boolean[] array) Shuffles the elements of the specified array using the same algorithm asCollections.shuffle(java.util.List<?>)
.static void
shuffle
(byte[] array) Shuffles the elements of the specified array using the same algorithm asCollections.shuffle(java.util.List<?>)
.static void
shuffle
(double[] array) Shuffles the elements of the specified array using the same algorithm asCollections.shuffle(java.util.List<?>)
.static void
shuffle
(float[] array) Shuffles the elements of the specified array using the same algorithm asCollections.shuffle(java.util.List<?>)
.static void
shuffle
(int[] array) Shuffles the elements of the specified array using the same algorithm asCollections.shuffle(java.util.List<?>)
.static void
shuffle
(long[] array) Shuffles the elements of the specified array using the same algorithm asCollections.shuffle(java.util.List<?>)
.static void
shuffle
(short[] array) Shuffles the elements of the specified array using the same algorithm asCollections.shuffle(java.util.List<?>)
.static <T> void
Shuffles the elements of the specified list by invoking theCollections.shuffle(java.util.List<?>)
method with the internalRandom
in this PRNG.static <T> void
shuffle
(T[] array) Shuffles the elements of the specified array using the same algorithm asCollections.shuffle(java.util.List<?>)
.
-
Method Details
-
setSeed
public static void setSeed(long seed) While the preferred method for seeding PRNGs is through thesetRandom
method since methods providing more entropy may be available to specific implementations, this method is intended to provide a uniform interface for setting the seed.- Parameters:
seed
- the new seed
-
setRandom
Sets the source of randomness to be used.- Parameters:
random
- the source of randomness to be used
-
getRandom
Returns the source of randomness currently used.- Returns:
- the source of randomness currently used
-
nextFloat
public static float nextFloat()Returns the next random, uniformly distributedfloat
value between 0.0 and 1.0.- Returns:
- the next random, uniformly distributed
float
value between 0.0 and 1.0
-
nextFloat
public static float nextFloat(float min, float max) Returns the next random, uniformly distributedfloat
value betweenmin
andmax
.- Parameters:
min
- the minimum value (inclusive)max
- the maximum value (exclusive)- Returns:
- the next random, uniformly distributed
float
value betweenmin
andmax
-
nextDouble
public static double nextDouble()Returns the next random, uniformly distributeddouble
value between 0.0 and 1.0.- Returns:
- the next random, uniformly distributed
double
value between 0.0 and 1.0
-
nextDouble
public static double nextDouble(double min, double max) Returns the next random, uniformly distributeddouble
value betweenmin
andmax
.- Parameters:
min
- the minimum value (inclusive)max
- the maximum value (exclusive)- Returns:
- the next random, uniformly distributed
double
value betweenmin
andmax
-
nextInt
public static int nextInt()Returns the next random, uniformly distributedint
value betweenInteger.MIN_VALUE
andInteger.MAX_VALUE
.- Returns:
- the next random, uniformly distributed
int
value betweenInteger.MIN_VALUE
andInteger.MAX_VALUE
.
-
nextInt
public static int nextInt(int n) Returns the next random, uniformly distributedint
value between0
(inclusive) andn
(exclusive).- Parameters:
n
- the maximum value (exclusive)- Returns:
- the next random, uniformly distributed
int
value between0
(inclusive) andn
(exclusive).
-
nextInt
public static int nextInt(int min, int max) Returns the next random, uniformly distributedint
value betweenmin
andmax
(both inclusive).- Parameters:
min
- the minimum value (inclusive)max
- the maximum value (inclusive)- Returns:
- the next random, uniformly distributed
int
value betweenmin
andmax
(both inclusive).
-
nextBoolean
public static boolean nextBoolean()Returns the next random, uniformly distributedboolean
value.- Returns:
- the next random, uniformly distributed
boolean
value.
-
nextGaussian
public static double nextGaussian()Returns the next random, Gaussian distributeddouble
value with mean0.0
and standard deviation1.0
.- Returns:
- the next random, Gaussian distributed
double
value with mean0.0
and standard deviation1.0
.
-
nextGaussian
public static double nextGaussian(double mean, double stdev) Returns the next random, Gaussian distributeddouble
value with meanmean
and standard deviationstdev
.- Parameters:
mean
- the meanstdev
- the standard deviation- Returns:
- the next random, Gaussian distributed
double
value with meanmean
and standard deviationstdev
.
-
shuffle
public static <T> void shuffle(T[] array) Shuffles the elements of the specified array using the same algorithm asCollections.shuffle(java.util.List<?>)
.- Type Parameters:
T
- the type of element stored in the array- Parameters:
array
- the array to be shuffled
-
shuffle
public static void shuffle(double[] array) Shuffles the elements of the specified array using the same algorithm asCollections.shuffle(java.util.List<?>)
.- Parameters:
array
- the array to be shuffled
-
shuffle
public static void shuffle(float[] array) Shuffles the elements of the specified array using the same algorithm asCollections.shuffle(java.util.List<?>)
.- Parameters:
array
- the array to be shuffled
-
shuffle
public static void shuffle(long[] array) Shuffles the elements of the specified array using the same algorithm asCollections.shuffle(java.util.List<?>)
.- Parameters:
array
- the array to be shuffled
-
shuffle
public static void shuffle(int[] array) Shuffles the elements of the specified array using the same algorithm asCollections.shuffle(java.util.List<?>)
.- Parameters:
array
- the array to be shuffled
-
shuffle
public static void shuffle(short[] array) Shuffles the elements of the specified array using the same algorithm asCollections.shuffle(java.util.List<?>)
.- Parameters:
array
- the array to be shuffled
-
shuffle
public static void shuffle(byte[] array) Shuffles the elements of the specified array using the same algorithm asCollections.shuffle(java.util.List<?>)
.- Parameters:
array
- the array to be shuffled
-
shuffle
public static void shuffle(boolean[] array) Shuffles the elements of the specified array using the same algorithm asCollections.shuffle(java.util.List<?>)
.- Parameters:
array
- the array to be shuffled
-
shuffle
Shuffles the elements of the specified list by invoking theCollections.shuffle(java.util.List<?>)
method with the internalRandom
in this PRNG.- Type Parameters:
T
- the type of elements stored in the list- Parameters:
list
- the list to be shuffled
-
nextItem
Returns a randomly selected item from the specified list.- Type Parameters:
T
- the type of the elements stored in the list- Parameters:
list
- the list from which the item is randomly selected- Returns:
- a randomly selected item from the specified list
-
nextItem
Returns a randomly selected item from the specified set.- Type Parameters:
T
- the type of the elements stored in the set- Parameters:
set
- the set from which the item is randomly selected- Returns:
- a randomly selected item from the specified set
-