public class TrimRNG extends java.lang.Object implements RandomnessSource, java.io.Serializable
long
states; one is a simple counter, and the rest mix and match all four states
to get their next value. This has been backported from jdkgdxds. This generator has a known minimum period of at
least 2 to the 64 and is statistically extremely likely to be much longer; no combinations of states are known that
put it in a bad starting state, and none are possible that reduce period below 2 to the 64. It uses no multiplication
and actually only needs addition, bitwise-rotation, and XOR to run; this may help on some exotic hardware.
FourWheelRNG
is faster on desktop hardware.
Another RandomnessSource that avoids multiplication, StrangerRNG
, is not as fast as this (in general). It has
passed 64TB of PractRand testing with no anomalies, and 300TB of hwd testing.
Modifier and Type | Field and Description |
---|---|
long |
stateA
Can be any long value.
|
long |
stateB
Can be any long value.
|
long |
stateC
Can be any long value.
|
long |
stateD
Can be any long value.
|
Constructor and Description |
---|
TrimRNG()
Creates a new generator seeded using Math.random.
|
TrimRNG(long seed)
Creates a new generator by passing the seed to
setSeed(long) . |
TrimRNG(long seedA,
long seedB,
long seedC,
long seedD)
Creates a new generator by specifying each state exactly.
|
Modifier and Type | Method and Description |
---|---|
TrimRNG |
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 |
getStateA()
Get the "A" part of the internal state as a long.
|
long |
getStateB()
Get the "B" part of the internal state as a long.
|
long |
getStateC()
Get the "C" part of the internal state as a long.
|
long |
getStateD()
Get the "D" part of the internal state as a long.
|
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.
|
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.
|
long |
previousLong() |
void |
setSeed(long seed)
This initializes all 4 states of the generator to random values based on the given seed.
|
void |
setStateA(long stateA)
Set the "A" part of the internal state with a long.
|
void |
setStateB(long stateB)
Set the "B" part of the internal state with a long.
|
void |
setStateC(long stateC)
Set the "C" part of the internal state with a long.
|
void |
setStateD(long stateD)
Set the "D" part of the internal state with a long.
|
java.lang.String |
toString() |
public long stateA
public long stateB
public long stateC
public long stateD
public TrimRNG()
public TrimRNG(long seed)
setSeed(long)
.seed
- any longpublic TrimRNG(long seedA, long seedB, long seedC, long seedD)
seedA
- any longseedB
- any longseedC
- any longseedD
- any longpublic void setSeed(long seed)
nextLong()
.
seed
, then only does a little distribution of the
mixed long so that 128 of 256 bits are always set across the four states.seed
- the initial seed; may be any longpublic long getStateA()
public void setStateA(long stateA)
stateA
- any 64-bit longpublic long getStateB()
public void setStateB(long stateB)
stateB
- any 64-bit longpublic long getStateC()
public void setStateC(long stateC)
stateC
- any 64-bit longpublic long getStateD()
public void setStateD(long stateD)
stateD
- any 64-bit longpublic long nextLong()
RandomnessSource
nextLong
in interface RandomnessSource
public long previousLong()
public int next(int bits)
RandomnessSource
next
in interface RandomnessSource
bits
- the number of bits to be returnedpublic TrimRNG copy()
copy
in interface RandomnessSource
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.