public class Lathe32RNG extends java.lang.Object implements StatefulRandomness, java.io.Serializable
setSeed(int)
, which needs to
use a different algorithm to spread a seed out across twice as much state), like xoroshiro and much of the xorshift
family of generators, and any arithmetic it performs is safe for GWT. Lathe makes an extremely small set of changes
to xoroshiro64+, running xoroshiro64+ as normal (holding on to the result as well as the initial stateA, called s[0]
in the original xoroshiro code) and then bitwise-rotating the result and adding the (now previous) stateA. Although
no bits of xoroshiro are truly free of artifacts, some are harder to find issues with
(see this article by PCG-Random's author
for more detail). It is unclear if the changes made here would improve the larger-state version, but they probably
would help to some extent with at least the binary rank failures. The period is identical to xoroshiro with two
32-bit states, at 0xFFFFFFFFFFFFFFFF or 2 to the 64 minus 1. This generator is slightly slower than xoroshiro without
the small extra steps applied to the output, but about as fast as Oriole32RNG
(this has a smaller period and
smaller state but implements StatefulRandomness). Some simple tests on bytes instead of ints showed that the
technique used here produces all possible bytes with equal frequency when run on bytes as state, with the exception
of producing 0 one less time (because both states cannot be 0 at the same time). This gives some confidence for the
algorithm used here, but the algorithm is still only one-dimensionally equidistributed (the same as xoroshiro128+),
meaning it produces some pairs of ints more frequently than others. You may want to prefer Starfish32RNG
,
which is the current default in GWTRNG
, because it can produce all pairs of ints and all longs (except one),
and has noticeably better quality even on some short generated sequences.
Constructor and Description |
---|
Lathe32RNG()
Creates a new generator seeded using two calls to Math.random().
|
Lathe32RNG(int seed)
Constructs this Lathe32RNG by dispersing the bits of seed using
setSeed(int) across the two parts of state
this has. |
Lathe32RNG(int stateA,
int stateB)
Constructs this Lathe32RNG 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 unless they are both 0). |
Lathe32RNG(long seed)
Constructs this Lathe32RNG by splitting the given seed across the two parts of state this has with
setState(long) . |
Modifier and Type | Method and Description |
---|---|
Lathe32RNG |
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.
|
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)
Using this method, any algorithm that might use the built-in Java Random
can interface with this randomness source.
|
int |
nextInt()
Can return any int, positive or negative, of any size permissible in a 32-bit signed integer.
|
long |
nextLong()
Using this method, any algorithm that needs to efficiently generate more
than 32 bits of random data can interface with this randomness source.
|
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 Lathe32RNG with three ints, where stateA and stateB can each be any int
unless they are both 0 (which will be treated as if stateA is 1 and stateB is 0).
|
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.lang.String |
toString() |
public Lathe32RNG()
public Lathe32RNG(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 Lathe32RNG(long seed)
setState(long)
.seed
- a long that will be split across both components of statepublic Lathe32RNG(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 unless they are both 0).stateA
- the number to use as the first part of the state; this will be 1 instead if both seeds are 0stateB
- the number to use as the second part of the statepublic int next(int bits)
RandomnessSource
next
in interface RandomnessSource
bits
- the number of bits to be returnedpublic int nextInt()
public long nextLong()
RandomnessSource
nextLong
in interface RandomnessSource
public Lathe32RNG copy()
copy
in interface RandomnessSource
copy
in interface StatefulRandomness
public void setSeed(int seed)
seed
- the int to use to produce this generator's statepublic int getStateA()
public void setStateA(int stateA)
setState(int, int)
to set both states at once, but the result will be the same if you call
setStateA() and then setStateB() or if you call setStateB() and then setStateA().stateA
- any intpublic int getStateB()
public void setStateB(int stateB)
setState(int, int)
to set both states at once, but the result will be the same if you call
setStateA() and then setStateB() or if you call setStateB() and then setStateA().stateB
- any intpublic void setState(int stateA, int stateB)
stateA
- any int (if stateA and stateB are both 0, this will be treated as 1)stateB
- 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 java.lang.String toString()
toString
in class java.lang.Object
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
Copyright © Eben Howard 2012–2022. All rights reserved.