public enum RandomSource extends java.lang.Enum<RandomSource>
Usage examples:
UniformRandomProvider rng = RandomSource.create(RandomSource.MT);
or
final int[] seed = new int[] { 196, 9, 0, 226 };
UniformRandomProvider rng = RandomSource.create(RandomSource.MT, seed);
or
final int[] seed = RandomSource.createIntArray(256);
UniformRandomProvider rng = RandomSource.create(RandomSource.MT, seed);
where the first argument to method create
is the identifier
of the generator's concrete implementation, and the second the is the
(optional) seed.
In the first form, a random seed will be generated
automatically
; in the second form, a fixed seed is used; a random seed
is explicitly generated in the third form.
Seeding is the procedure by which a value (or set of values) is used to initialize a generator instance. The requirement that a given seed will always result in the same internal state allows to create different instances of a generator that will produce the same sequence of pseudo-random numbers.
The type of data used as a seed depends on the concrete implementation
as some types may not provide enough information to fully initialize
the generator's internal state.
The reference algorithm's seeding procedure (if provided) operates
on a value of a (single) native type:
Each concrete implementation's constructor creates an instance using
the native type whose information contents is used to set the
internal state.
When the seed value passed by the caller is of the native type, it is
expected that the sequences produced will be identical to those
produced by other implementations of the same reference algorithm.
However, when the seed value passed by the caller is not of the native
type, a transformation is performed by this library and the resulting
native type value will not contain more information than the
original seed value.
If the algorithm's native type is "simpler" than the type passed by
the caller, then some (unused) information will even be lost.
The transformation from non-native to native seed type is arbitrary,
as long as it does not reduce the amount of information required by
the algorithm to initialize its state.
The consequence of the transformation is that sequences produced
by this library may not be the same as the sequences produced
by other implementations of the same algorithm!
For each algorithm, the Javadoc mentions the "ideal" size of the seed,
meaning the number of int
or long
values that is neither
too large (i.e. some of the seed is useless) or too small (i.e. an
internal procedure will fill the state with redundant information
computed from the given seed).
Note that some algorithms are inherently sensitive to having too low diversity in their initial state. For example, it is often a bad idea to use a seed that is mostly composed of zeroes, or of repeated values.
This class provides methods to generate random seeds (single values
or arrays of values, of int
or long
types) that can
be passed to the generators factory method
.
Although the seed-generating methods defined in this class will likely return different values each time they are called, there is no guarantee:
The current implementations have no provision for producing non-overlapping
sequences.
For parallel applications, a possible workaround is that each thread uses
a generator of a different type (see TWO_CMRES_SELECT
).
Note: Seeding is not equivalent to restoring the internal state of an already initialized generator. Indeed, generators can have a state that is more complex than the seed, and seeding is thus a transformation (from seed to state). Implementations do not provide the inverse transformation (from state to seed), hence it is not generally possible to know the seed that would initialize a new generator instance to the current state of another instance. Reseeding is also inefficient if the purpose is to continue the same sequence where another instance left off, as it would require to "replay" all the calls performed by that other instance (and it would require to know the number of calls to the primary source of randomness, which is also not usually accessible).
Enum Constant | Description |
---|---|
ISAAC |
Source of randomness is
ISAACRandom . |
JDK |
Source of randomness is
JDKRandom . |
KISS |
Source of randomness is
KISSRandom . |
MT |
Source of randomness is
MersenneTwister . |
MT_64 |
Source of randomness is
MersenneTwister64 . |
MWC_256 |
Source of randomness is
MultiplyWithCarry256 . |
SPLIT_MIX_64 |
Source of randomness is
SplitMix64 . |
TWO_CMRES |
Source of randomness is
TwoCmres . |
TWO_CMRES_SELECT |
Source of randomness is
TwoCmres ,
with explicit selection of the two subcycle generators. |
WELL_1024_A |
Source of randomness is
Well1024a . |
WELL_19937_A |
Source of randomness is
Well19937a . |
WELL_19937_C |
Source of randomness is
Well19937c . |
WELL_44497_A |
Source of randomness is
Well44497a . |
WELL_44497_B |
Source of randomness is
Well44497b . |
WELL_512_A |
Source of randomness is
Well512a . |
XOR_SHIFT_1024_S |
Source of randomness is
XorShift1024Star . |
Modifier and Type | Method | Description |
---|---|---|
static org.apache.commons.rng.RestorableUniformRandomProvider |
create(RandomSource source) |
Creates a random number generator with a random seed.
|
static org.apache.commons.rng.RestorableUniformRandomProvider |
create(RandomSource source,
java.lang.Object seed,
java.lang.Object... data) |
Creates a random number generator with the given
seed . |
static int |
createInt() |
Creates a number for use as a seed.
|
static int[] |
createIntArray(int n) |
Creates an array of numbers for use as a seed.
|
static long |
createLong() |
Creates a number for use as a seed.
|
static long[] |
createLongArray(int n) |
Creates an array of numbers for use as a seed.
|
boolean |
isNativeSeed(java.lang.Object seed) |
Checks whether the type of given
seed is the native type
of the implementation. |
static org.apache.commons.rng.UniformRandomProvider |
unrestorable(org.apache.commons.rng.UniformRandomProvider delegate) |
Wraps the given
delegate generator in a new instance that
does not allow access to the "save/restore" functionality. |
static RandomSource |
valueOf(java.lang.String name) |
Returns the enum constant of this type with the specified name.
|
static RandomSource[] |
values() |
Returns an array containing the constants of this enum type, in
the order they are declared.
|
public static final RandomSource JDK
JDKRandom
.
Long
.public static final RandomSource WELL_512_A
Well512a
.
int[]
.public static final RandomSource WELL_1024_A
Well1024a
.
int[]
.public static final RandomSource WELL_19937_A
Well19937a
.
int[]
.public static final RandomSource WELL_19937_C
Well19937c
.
int[]
.public static final RandomSource WELL_44497_A
Well44497a
.
int[]
.public static final RandomSource WELL_44497_B
Well44497b
.
int[]
.public static final RandomSource MT
MersenneTwister
.
int[]
.public static final RandomSource ISAAC
ISAACRandom
.
int[]
.public static final RandomSource SPLIT_MIX_64
SplitMix64
.
Long
.public static final RandomSource XOR_SHIFT_1024_S
XorShift1024Star
.
long[]
.public static final RandomSource TWO_CMRES
TwoCmres
.
This generator is equivalent to TWO_CMRES_SELECT
with the choice of the
pair (0, 1)
for the two subcycle generators.
Integer
.public static final RandomSource TWO_CMRES_SELECT
TwoCmres
,
with explicit selection of the two subcycle generators.
The selection of the subcycle generator is by passing its index in the internal
table, a value between 0 (included) and 13 (included).
The two indices must be different.
Different choices of an ordered pair of indices create independent generators.
Integer
.public static final RandomSource MT_64
MersenneTwister64
.
long[]
.public static final RandomSource MWC_256
MultiplyWithCarry256
.
int[]
.public static final RandomSource KISS
KISSRandom
.
int[]
.public static RandomSource[] values()
for (RandomSource c : RandomSource.values()) System.out.println(c);
public static RandomSource valueOf(java.lang.String name)
name
- the name of the enum constant to be returned.java.lang.IllegalArgumentException
- if this enum type has no constant with the specified namejava.lang.NullPointerException
- if the argument is nullpublic boolean isNativeSeed(java.lang.Object seed)
seed
is the native type
of the implementation.seed
- Seed value.true
if the type of seed
is the native
type for this RNG source.public static org.apache.commons.rng.RestorableUniformRandomProvider create(RandomSource source)
Usage example:
UniformRandomProvider rng = RandomSource.create(RandomSource.MT);
or, if a "save/restore"
functionality is needed,
RestorableUniformRandomProvider rng = RandomSource.create(RandomSource.MT);
source
- RNG type.create(RandomSource,Object,Object[])
public static org.apache.commons.rng.RestorableUniformRandomProvider create(RandomSource source, java.lang.Object seed, java.lang.Object... data)
seed
.
Usage example:
UniformRandomProvider rng = RandomSource.create(RandomSource.TWO_CMRES_SELECT, 26219, 6, 9);
Valid types for the seed
are:
Integer
(or int
)Long
(or long
)int[]
long[]
byte[]
Notes:
native type
, the conversion of a
set of different seeds will necessarily result in the same value
of the native seed type.
seed
is
null
, the size of the generated array will be 128.
source
- RNG type.seed
- Seed value. It can be null
(in which case a
random value will be used).data
- Additional arguments to the implementation's constructor.
Please refer to the documentation of each specific implementation.java.lang.UnsupportedOperationException
- if the type of the seed
is invalid.java.lang.IllegalStateException
- if data is missing to initialize the
generator implemented by the given source
.create(RandomSource)
public static int createInt()
public static long createLong()
public static int[] createIntArray(int n)
n
- Size of the array to create.n
random numbers.public static long[] createLongArray(int n)
n
- Size of the array to create.n
random numbers.public static org.apache.commons.rng.UniformRandomProvider unrestorable(org.apache.commons.rng.UniformRandomProvider delegate)
delegate
generator in a new instance that
does not allow access to the "save/restore" functionality.delegate
- Generator to which calls will be delegated.Copyright © 2016–2018 The Apache Software Foundation. All rights reserved.