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 RandomReturns the source of randomness currently used.static booleanReturns the next random, uniformly distributedbooleanvalue.static doubleReturns the next random, uniformly distributeddoublevalue between 0.0 and 1.0.static doublenextDouble(double min, double max) Returns the next random, uniformly distributeddoublevalue betweenminandmax.static floatReturns the next random, uniformly distributedfloatvalue between 0.0 and 1.0.static floatnextFloat(float min, float max) Returns the next random, uniformly distributedfloatvalue betweenminandmax.static doubleReturns the next random, Gaussian distributeddoublevalue with mean0.0and standard deviation1.0.static doublenextGaussian(double mean, double stdev) Returns the next random, Gaussian distributeddoublevalue with meanmeanand standard deviationstdev.static intnextInt()Returns the next random, uniformly distributedintvalue betweenInteger.MIN_VALUEandInteger.MAX_VALUE.static intnextInt(int n) Returns the next random, uniformly distributedintvalue between0(inclusive) andn(exclusive).static intnextInt(int min, int max) Returns the next random, uniformly distributedintvalue betweenminandmax(both inclusive).static <T> TReturns a randomly selected item from the specified list.static <T> TReturns a randomly selected item from the specified set.static voidSets the source of randomness to be used.static voidsetSeed(long seed) While the preferred method for seeding PRNGs is through thesetRandommethod 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 voidshuffle(boolean[] array) Shuffles the elements of the specified array using the same algorithm asCollections.shuffle(java.util.List<?>).static voidshuffle(byte[] array) Shuffles the elements of the specified array using the same algorithm asCollections.shuffle(java.util.List<?>).static voidshuffle(double[] array) Shuffles the elements of the specified array using the same algorithm asCollections.shuffle(java.util.List<?>).static voidshuffle(float[] array) Shuffles the elements of the specified array using the same algorithm asCollections.shuffle(java.util.List<?>).static voidshuffle(int[] array) Shuffles the elements of the specified array using the same algorithm asCollections.shuffle(java.util.List<?>).static voidshuffle(long[] array) Shuffles the elements of the specified array using the same algorithm asCollections.shuffle(java.util.List<?>).static voidshuffle(short[] array) Shuffles the elements of the specified array using the same algorithm asCollections.shuffle(java.util.List<?>).static <T> voidShuffles the elements of the specified list by invoking theCollections.shuffle(java.util.List<?>)method with the internalRandomin this PRNG.static <T> voidshuffle(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 thesetRandommethod 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 distributedfloatvalue between 0.0 and 1.0.- Returns:
- the next random, uniformly distributed
floatvalue between 0.0 and 1.0
-
nextFloat
public static float nextFloat(float min, float max) Returns the next random, uniformly distributedfloatvalue betweenminandmax.- Parameters:
min- the minimum value (inclusive)max- the maximum value (exclusive)- Returns:
- the next random, uniformly distributed
floatvalue betweenminandmax
-
nextDouble
public static double nextDouble()Returns the next random, uniformly distributeddoublevalue between 0.0 and 1.0.- Returns:
- the next random, uniformly distributed
doublevalue between 0.0 and 1.0
-
nextDouble
public static double nextDouble(double min, double max) Returns the next random, uniformly distributeddoublevalue betweenminandmax.- Parameters:
min- the minimum value (inclusive)max- the maximum value (exclusive)- Returns:
- the next random, uniformly distributed
doublevalue betweenminandmax
-
nextInt
public static int nextInt()Returns the next random, uniformly distributedintvalue betweenInteger.MIN_VALUEandInteger.MAX_VALUE.- Returns:
- the next random, uniformly distributed
intvalue betweenInteger.MIN_VALUEandInteger.MAX_VALUE.
-
nextInt
public static int nextInt(int n) Returns the next random, uniformly distributedintvalue between0(inclusive) andn(exclusive).- Parameters:
n- the maximum value (exclusive)- Returns:
- the next random, uniformly distributed
intvalue between0(inclusive) andn(exclusive).
-
nextInt
public static int nextInt(int min, int max) Returns the next random, uniformly distributedintvalue betweenminandmax(both inclusive).- Parameters:
min- the minimum value (inclusive)max- the maximum value (inclusive)- Returns:
- the next random, uniformly distributed
intvalue betweenminandmax(both inclusive).
-
nextBoolean
public static boolean nextBoolean()Returns the next random, uniformly distributedbooleanvalue.- Returns:
- the next random, uniformly distributed
booleanvalue.
-
nextGaussian
public static double nextGaussian()Returns the next random, Gaussian distributeddoublevalue with mean0.0and standard deviation1.0.- Returns:
- the next random, Gaussian distributed
doublevalue with mean0.0and standard deviation1.0.
-
nextGaussian
public static double nextGaussian(double mean, double stdev) Returns the next random, Gaussian distributeddoublevalue with meanmeanand standard deviationstdev.- Parameters:
mean- the meanstdev- the standard deviation- Returns:
- the next random, Gaussian distributed
doublevalue with meanmeanand 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 internalRandomin 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
-