public class XoshiroStarPhi32RNG extends java.lang.Object implements RandomnessSource, java.io.Serializable
nextInt()
, but also that when up to four 32-bit values are
generated and treated as up to a 128-bit output, then all possible 128-bit outputs are equally likely (with the
exception of the 128-bit value 0x9E3779BD9E3779BD9E3779BD9E3779BD, which won't ever be generated as a group even
though 0x9E3779BD can occur up to three times in four results). The scrambler simply multiplies a state variable by
31, rotates that value left by 23, and adds a number obtained from the golden ratio, phi (0x9E3779BD). It may have
all sorts of issues since this scrambler hasn't been analyzed much, but 128 bits of state help make most issues less
severe, and the same scrambler works well for xoroshiro with 32-bit states (used in Starfish32RNG
). A
clear known flaw is that if you subtract the same golden-ratio-based number from each result, the resulting modified
generator will quickly fail binary matrix rank tests. This could be ameliorated by employing a fifth state variable
that increments in a Weyl sequence, which is what Oriole32RNG
does, and adding that instead of the golden
ratio, though this would eliminate the 4-dimensional equidistribution. XoshiroStarPhi32RNG is optimized for GWT,
like Lathe32RNG
and Starfish32RNG
, which means any non-bitwise math in the source is followed
by bitwise math later, and this sometimes can result in obtuse-looking code along the lines of
int foo = bar + 0x9E3779BD | 0;
.
Constructor and Description |
---|
XoshiroStarPhi32RNG()
Creates a new generator seeded using four calls to Math.random().
|
XoshiroStarPhi32RNG(int seed)
Constructs this XoshiroStarPhi32RNG by dispersing the bits of seed using
setSeed(int) across the four
parts of state this has. |
XoshiroStarPhi32RNG(int stateA,
int stateB,
int stateC,
int stateD)
Constructs this XoshiroStarPhi32RNG by calling
setState(int, int, int, int) on stateA and stateB as
given; see that method for the specific details (the states are kept as-is unless they are all 0). |
XoshiroStarPhi32RNG(long seed)
Constructs this XoshiroStarPhi32RNG by dispersing the bits of seed using
setSeed(long) across the four
parts of state this has. |
Modifier and Type | Method and Description |
---|---|
XoshiroStarPhi32RNG |
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) |
int |
getStateA() |
int |
getStateB() |
int |
getStateC() |
int |
getStateD() |
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 a GWT-compatible variant of SplitMix32 four
times to get four ints of state, all guaranteed to be different.
|
void |
setSeed(long seed)
Sets the state of this generator using one long, running it through a GWT-compatible variant of SplitMix32 four
times to get four ints of state, guaranteed to repeat a state no more than two times.
|
void |
setState(int stateA,
int stateB,
int stateC,
int stateD)
Sets the current internal state of this XoshiroStarPhi32RNG with four ints, where each can be any int unless
they are all 0 (which will be treated as if stateA is 1 and the rest are 0).
|
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.
|
void |
setStateC(int stateC)
Sets the third part of the state to the given int.
|
void |
setStateD(int stateD)
Sets the second part of the state to the given int.
|
java.lang.String |
toString() |
public XoshiroStarPhi32RNG()
public XoshiroStarPhi32RNG(int seed)
setSeed(int)
across the four
parts of state this has.seed
- an int that won't be used exactly, but will affect all components of statepublic XoshiroStarPhi32RNG(long seed)
setSeed(long)
across the four
parts of state this has.seed
- a long that will be split across all components of statepublic XoshiroStarPhi32RNG(int stateA, int stateB, int stateC, int stateD)
setState(int, int, int, int)
on stateA and stateB as
given; see that method for the specific details (the states are kept as-is unless they are all 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 statestateC
- the number to use as the third part of the statestateD
- the number to use as the fourth 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 XoshiroStarPhi32RNG copy()
copy
in interface RandomnessSource
public void setSeed(int seed)
seed
- the int to use to produce this generator's statespublic void setSeed(long seed)
seed
- the long to use to produce this generator's statespublic int getStateA()
public void setStateA(int stateA)
setState(int, int, int, int)
to set all four states at once, but the result will be the same if you set
the four states individually.stateA
- any intpublic int getStateB()
public void setStateB(int stateB)
setState(int, int, int, int)
to set all four states at once, but the result will be the same if you set
the four states individually.stateB
- any intpublic int getStateC()
public void setStateC(int stateC)
setState(int, int, int, int)
to set all four states at once, but the result will be the same if you set
the four states individually.stateC
- any intpublic int getStateD()
public void setStateD(int stateD)
setState(int, int, int, int)
to set all four states at once, but the result will be the same if you set
the four states individually.stateD
- any intpublic void setState(int stateA, int stateB, int stateC, int stateD)
stateA
- any int (if all parameters are both 0, this will be treated as 1)stateB
- any intstateC
- any intstateD
- any intpublic 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.