public class SilkRNG extends AbstractRNG implements IStatefulRNG, java.io.Serializable
GWTRNG
but should perform better on recent desktop JVMs. This uses a
related algorithm to OrbitRNG
, modified to use 32-bit math and more stringently randomize output. It has two
32-bit ints for state and a period of 0x10000000000000000 (2 to the 64), while passing 32TB of PractRand tests
without any failures or anomalies (so its quality is very good). It is extremely fast when run on Java 13, at least
using OpenJ9 as the compiler; it can produce a billion ints a second on moderately-recent laptop hardware. A nice
quality of the implementation is that it allows any int for both of its states, so you don't need to check and avoid
setting both states to 0 (which GWTRNG has to do in its internals).
Constructor and Description |
---|
SilkRNG()
Creates a new generator seeded using two calls to Math.random().
|
SilkRNG(int seed)
Constructs this SilkRNG by dispersing the bits of seed using
setSeed(int) across the two parts of state
this has. |
SilkRNG(int stateA,
int stateB)
Constructs this SilkRNG by calling
setState(int, int) on stateA and stateB as given; see that method
for the specific details (stateA and stateB are kept as-is). |
SilkRNG(long seed)
Constructs this SilkRNG by splitting the given seed across the two parts of state this has with
setState(long) . |
SilkRNG(java.lang.String seed)
Hashes
seed using both CrossHash.hash(CharSequence) and String.hashCode() and uses those
two results as the two states with setState(int, int) . |
Modifier and Type | Method and Description |
---|---|
SilkRNG |
copy()
Creates a copy of this SilkRNG; it will generate the same random numbers, given the same calls in order, as this
SilkRNG at the point copy() is called.
|
static long |
determine(int state)
A deterministic random long generator that, given one int
state as input, returns an
almost-always-different long as a result. |
static int |
determineBounded(int state,
int bound)
A deterministic random int generator that, given one int
state and an outer int bound as input,
returns an int between 0 (inclusive) and bound (exclusive) as a result, which should have no noticeable
correlation between state and the result. |
static double |
determineDouble(int state)
A deterministic random double generator that, given one int
state as input, returns an
almost-always-different double between 0.0 and 1.0 as a result. |
static float |
determineFloat(int state)
A deterministic random float generator that, given one int
state as input, returns an
almost-always-different float between 0.0f and 1.0f as a result. |
static int |
determineInt(int state)
A deterministic random int generator that, given one int
state as input, irreversibly returns an
almost-always-different int as a result. |
boolean |
equals(java.lang.Object o) |
long |
getState()
Get the current internal state of the StatefulRandomness as a long.
|
int |
getStateA() |
int |
getStateB() |
int |
hashCode() |
int |
next(int bits)
Get up to 32 bits (inclusive) of random output; the int this produces
will not require more than
bits bits to represent. |
boolean |
nextBoolean()
Get a random bit of state, interpreted as true or false with approximately equal likelihood.
|
double |
nextDouble()
Gets a random double between 0.0 inclusive and 1.0 exclusive.
|
float |
nextFloat()
Gets a random float between 0.0f inclusive and 1.0f exclusive.
|
int |
nextInt()
Get a random integer between Integer.MIN_VALUE to Integer.MAX_VALUE (both inclusive).
|
int |
nextInt(int bound)
Returns a random non-negative integer below the given bound, or 0 if the bound is 0 or
negative.
|
long |
nextLong()
Get a random long between Long.MIN_VALUE to Long.MAX_VALUE (both inclusive).
|
void |
setSeed(int seed)
Sets the state of this generator using one int, running it through Zog32RNG's algorithm two times to get
two ints.
|
void |
setState(int stateA,
int stateB)
Sets the current internal state of this SilkRNG with two ints, where stateA and stateB can each be any int.
|
void |
setState(long state)
Set the current internal state of this StatefulRandomness with a long.
|
void |
setStateA(int stateA)
Sets the first part of the state to the given int.
|
void |
setStateB(int stateB)
Sets the second part of the state to the given int.
|
java.io.Serializable |
toSerializable()
Gets a view of this IRNG in a way that implements
Serializable , which is simply this IRNG. |
java.lang.String |
toString() |
between, between, between, getRandomElement, getRandomElement, getRandomElement, nextDouble, nextFloat, nextLong, nextSignedInt, nextSignedLong, randomOrdering, randomOrdering, randomPortion, shuffle, shuffle, shuffle, shuffle, shuffleInPlace, shuffleInPlace, swap
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
between, between, between, getRandomElement, getRandomElement, getRandomElement, nextDouble, nextFloat, nextLong, nextSignedInt, nextSignedLong, randomOrdering, randomOrdering, randomPortion, shuffle, shuffle, shuffle, shuffle, shuffleInPlace, shuffleInPlace
public SilkRNG()
public SilkRNG(int seed)
setSeed(int)
across the two parts of state
this has.seed
- an int that won't be used exactly, but will affect both components of statepublic SilkRNG(long seed)
setState(long)
.seed
- a long that will be split across both components of statepublic SilkRNG(int stateA, int stateB)
setState(int, int)
on stateA and stateB as given; see that method
for the specific details (stateA and stateB are kept as-is).stateA
- the number to use as the first part of the statestateB
- the number to use as the second part of the statepublic SilkRNG(java.lang.String seed)
seed
using both CrossHash.hash(CharSequence)
and String.hashCode()
and uses those
two results as the two states with setState(int, int)
. If seed is null, this won't call
String.hashCode() on it and will instead use 0 as that state.seed
- any String; may be nullpublic int next(int bits)
bits
bits to represent.next
in interface IRNG
next
in interface RandomnessSource
next
in class AbstractRNG
bits
- an int between 1 and 32, both inclusivepublic int nextInt()
nextInt
in interface IRNG
nextInt
in class AbstractRNG
public int nextInt(int bound)
nextInt
in interface IRNG
nextInt
in class AbstractRNG
bound
- the upper bound (exclusive)public long nextLong()
nextLong
in interface IRNG
nextLong
in interface RandomnessSource
nextLong
in class AbstractRNG
public boolean nextBoolean()
nextBoolean
in interface IRNG
nextBoolean
in class AbstractRNG
public double nextDouble()
nextDouble
in interface IRNG
nextDouble
in class AbstractRNG
public float nextFloat()
nextFloat
in interface IRNG
nextFloat
in class AbstractRNG
public SilkRNG copy()
copy
in interface RandomnessSource
copy
in interface StatefulRandomness
copy
in class AbstractRNG
public java.io.Serializable toSerializable()
Serializable
, which is simply this IRNG.toSerializable
in interface IRNG
toSerializable
in class AbstractRNG
Serializable
view of this IRNG or a similar one; always this
public void setSeed(int seed)
seed
- the int to use to produce this generator's statepublic int getStateA()
public void setStateA(int stateA)
stateA
- any intpublic int getStateB()
public void setStateB(int stateB)
stateB
- any intpublic void setState(int stateA, int stateB)
stateA
- any intstateB
- any intpublic long getState()
getState
in interface StatefulRandomness
public void setState(long state)
setState
in interface StatefulRandomness
state
- a 64-bit long. You should avoid passing 0; this implementation will treat it as 1.public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object
public static int determineInt(int state)
state
as input, irreversibly returns an
almost-always-different int as a result. Unlike the rest of SilkRNG, this will not produce all possible ints given
all ints as inputs, and probably a third of all possible ints cannot be returned. You should call this with
SilkRNG.determineInt(state = state + 1 | 0)
(you can subtract 1 to go backwards instead of forwards),
which will allow overflow in the incremented state to be handled the same on GWT as on desktop.state
- an int that should go up or down by 1 each call, as with SilkRNG.determineInt(state = state + 1 | 0)
to handle overflowstate
public static int determineBounded(int state, int bound)
state
and an outer int bound
as input,
returns an int between 0 (inclusive) and bound
(exclusive) as a result, which should have no noticeable
correlation between state
and the result. You should call this with
SilkRNG.determineBound(state = state + 1 | 0, bound)
(you can subtract 1 to go backwards instead of
forwards), which will allow overflow in the incremented state to be handled the same on GWT as on desktop.
Like most bounded int generation in SquidLib, this uses some long math, but most of the function uses ints.state
- an int that should go up or down by 1 each call, as with SilkRNG.determineBounded(state = state + 1 | 0, bound)
to handle overflowbound
- the outer exclusive bound, as an int; may be positive or negativebound
(exclusive)public static long determine(int state)
state
as input, returns an
almost-always-different long as a result. This can only return a tiny fraction of all possible longs, since there
are at most 2 to the 32 possible ints and this doesn't even return different values for each of those. You should
call this with SilkRNG.determine(state = state + 1 | 0)
(you can subtract 1 to go backwards instead of
forwards), which will allow overflow in the incremented state to be handled the same on GWT as on desktop.state
- an int that should go up or down by 1 each call, as with SilkRNG.determine(state = state + 1 | 0)
to handle overflowstate
public static float determineFloat(int state)
state
as input, returns an
almost-always-different float between 0.0f and 1.0f as a result. Unlike the rest of SilkRNG, this might not
produce all possible floats given all ints as inputs, and some fraction of possible floats cannot be returned.
You should call this with SilkRNG.determineFloat(state = state + 1 | 0)
(you can subtract 1 to go
backwards instead of forwards), which will allow overflow in the incremented state to be handled the same on GWT
as on desktop.state
- an int that should go up or down by 1 each call, as with SilkRNG.determineFloat(state = state + 1 | 0)
to handle overflowstate
public static double determineDouble(int state)
state
as input, returns an
almost-always-different double between 0.0 and 1.0 as a result. This cannot produce more than a tiny fraction of
all possible doubles because the input is 32 bits and at least 53 bits are needed to represent most doubles from
0.0 to 1.0. You should call this with SilkRNG.determineDouble(state = state + 1 | 0)
(you can subtract 1
to go backwards instead of forwards), which will allow overflow in the incremented state to be handled the same
on GWT as on desktop.state
- an int that should go up or down by 1 each call, as with SilkRNG.determineDouble(state = state + 1 | 0)
to handle overflowstate
Copyright © Eben Howard 2012–2022. All rights reserved.