public class PermutedRNG extends java.lang.Object implements RandomnessSource, StatefulRandomness, SkippingRandomness, java.io.Serializable
Modifier and Type | Field and Description |
---|---|
long |
state
The state can be seeded with any value.
|
Constructor and Description |
---|
PermutedRNG()
Creates a new generator seeded using Math.random.
|
PermutedRNG(long seed)
Constructs a new PermutedRNG with the given seed as its state, exactly.
|
Modifier and Type | Method and Description |
---|---|
PermutedRNG |
copy()
Produces a copy of this RandomnessSource that, if next() and/or nextLong() are called on this object and the
copy, both will generate the same sequence of random numbers from the point copy() was called.
|
static long |
determine(long state)
Given suitably-different inputs as
state , this will permute that state to get a seemingly-unrelated
number. |
static int |
determineBounded(long state,
int bound)
Given suitably-different inputs as
state , this will permute that state to get a seemingly-unrelated
number as an int between 0 and bound. |
long |
getState()
Gets the current state of this generator.
|
int |
next(int bits)
Gets a random int with at most the specified number of bits.
|
boolean |
nextBoolean()
Gets a random value, true or false.
|
void |
nextBytes(byte[] bytes)
Given a byte array as a parameter, this will fill the array with random bytes (modifying it
in-place).
|
double |
nextDouble()
Gets a uniform random double in the range
[0.0,1.0) . |
double |
nextDouble(double outer)
Gets a uniform random double in the range [0.0,outer) given a positive parameter outer.
|
float |
nextFloat()
Gets a uniform random float in the range
[0.0,1.0)
Calls nextLong() exactly once. |
int |
nextInt()
Can return any int, positive or negative, of any size permissible in a 32-bit signed integer.
|
int |
nextInt(int bound)
Exclusive on the outer bound; the inner bound is 0.
|
int |
nextInt(int lower,
int upper)
Inclusive lower, exclusive upper.
|
long |
nextLong()
Can return any long, positive or negative, of any size permissible in a 64-bit signed integer.
|
long |
nextLong(long bound)
Exclusive on the outer bound; the inner bound is 0.
|
long |
nextLong(long inner,
long outer)
Inclusive inner, exclusive outer; both inner and outer can be positive or negative.
|
void |
setSeed(long seed)
Sets the seed of this generator (which is also the current state).
|
void |
setState(long seed)
Sets the seed (also the current state) of this generator.
|
long |
skip(long advance)
Advances or rolls back the PermutedRNG's state without actually generating each number.
|
java.lang.String |
toString() |
public PermutedRNG()
public PermutedRNG(long seed)
seed
- a long that will be used as-is for the state of a new PermutedRNGpublic int next(int bits)
nextLong()
exactly one time.next
in interface RandomnessSource
bits
- the number of bits to be returned, between 1 and 32public int nextInt()
nextLong()
exactly one time.public long nextLong()
nextLong
in interface RandomnessSource
public PermutedRNG copy()
copy
in interface RandomnessSource
copy
in interface StatefulRandomness
public int nextInt(int bound)
nextLong()
exactly once.
The bound should not be negative; use IRNG.nextSignedInt(int)
if you need a negative outer bound.bound
- the upper bound; should be positivepublic int nextInt(int lower, int upper)
nextLong()
exactly once.lower
- the lower bound, inclusive, can be positive or negativeupper
- the upper bound, exclusive, can be positive or negativepublic long nextLong(long bound)
nextLong()
exactly once.bound
- the outer exclusive bound; may be positive or negativepublic long nextLong(long inner, long outer)
nextLong()
exactly once.inner
- the inner bound, inclusive, can be positive or negativeouter
- the outer bound, exclusive, can be positive or negative and may be greater than or less than innerpublic double nextDouble()
[0.0,1.0)
.
Calls nextLong()
exactly once.public double nextDouble(double outer)
nextLong()
exactly once.outer
- the exclusive outer bound, can be negativepublic float nextFloat()
[0.0,1.0)
Calls nextLong()
exactly once.public boolean nextBoolean()
nextLong()
exactly once.public void nextBytes(byte[] bytes)
nextLong()
Math.ceil(bytes.length / 8.0)
times.bytes
- a byte array that will have its contents overwritten with random bytes.public void setSeed(long seed)
seed
- the seed to use for this PermutedRNG, as if it was constructed with this seed.public void setState(long seed)
setState
in interface StatefulRandomness
seed
- the seed to use for this PermutedRNG, as if it was constructed with this seed.public long getState()
getState
in interface StatefulRandomness
nextLong()
public long skip(long advance)
nextLong()
,
and returns the random number produced at that step (you can get the state with getState()
).
Skipping ahead or behind takes more than constant time, unlike with LightRNG
, but less time
than calling nextLong()
advance
times. Skipping backwards by one step is the worst case for this
technique.skip
in interface SkippingRandomness
advance
- Number of future generations to skip past. Can be negative to backtrack.public java.lang.String toString()
toString
in class java.lang.Object
public static long determine(long state)
state
, this will permute that state to get a seemingly-unrelated
number. Unlike LightRNG.determine(long)
, this will not work with inputs that are sequential, and it is
recommended that subsequent calls change state with a linear congruential generator like
PermutedRNG.determine(state = state * 0x5851F42D4C957F2DL + 0x14057B7EF767814FL)
. It will be correct for
any inputs, but if state
is 0, then this will return 0.state
- a long that should be changed with state = state * 0x5851F42D4C957F2DL + 0x14057B7EF767814FL
public static int determineBounded(long state, int bound)
state
, this will permute that state to get a seemingly-unrelated
number as an int between 0 and bound. Unlike LightRNG.determine(long)
, this will not work with inputs
that are sequential, and it is recommended that subsequent calls change state with a linear congruential
generator like PermutedRNG.determine(state = state * 0x5851F42D4C957F2DL + 0x14057B7EF767814FL)
. It will
be correct for any inputs, but if state
is 0, then this will return 0.state
- a long that should be changed with state = state * 0x5851F42D4C957F2DL + 0x14057B7EF767814FL
bound
- the exclusive outer bound on the numbers this can produce, as an intCopyright © Eben Howard 2012–2022. All rights reserved.