public class GearRNG extends java.lang.Object implements RandomnessSource, java.io.Serializable
TangleRNG
, which also has two states (one odd) and uses a
very similar algorithm, but it never "shifts gears," which drops its period down to 2 to the 64, makes it a
SkippingRandomness
, and should in theory speed it up (in practice, TangleRNG needs an extra step that actually
makes it slower than GearRNG). An individual TangleRNG can't produce all possible long outputs and can produce some
duplicates, but each pair of states for a TangleRNG has a different set of which outputs will be skipped and which
will be duplicated. Since it would require months of solid number generation to exhaust the period of a TangleRNG, and
that's the only time an output can be confirmed as skipped, it's probably fine for most usage to use many different
TangleRNGs, all seeded differently. Other choices: you could use one OrbitRNG
(which technically has a longer
period, but some states produce very similar output), DiverRNG
(if you don't mind that it never produces a
duplicate output), IsaacRNG
(if speed is less important but more secure output is), or Lathe64RNG, though all
of those are probably slower than using one GearRNG object or even many TangleRNG objects.
Constructor and Description |
---|
GearRNG()
Creates a new generator seeded using Math.random.
|
GearRNG(long seed) |
GearRNG(long seedA,
long seedB) |
Modifier and Type | Method and Description |
---|---|
GearRNG |
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.
|
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.
|
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; the lowest bit is always discarded and replaced with 1.
|
java.lang.String |
toString() |
public GearRNG()
public GearRNG(long seed)
public GearRNG(long seedA, long seedB)
public long getStateA()
public void setStateA(long stateA)
stateA
- any 64-bit longpublic long getStateB()
public void setStateB(long stateB)
stateB
- any 64-bit longpublic int next(int bits)
next
in interface RandomnessSource
bits
- the number of bits to be returnedpublic long nextLong()
Get a random long between Long.MIN_VALUE and Long.MAX_VALUE (both inclusive).
nextLong
in interface RandomnessSource
public GearRNG 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.