public class XoRoRNG extends java.lang.Object implements RandomnessSource, java.io.Serializable
DiverRNG
, which is the default for RNG
, will be faster and won't fail tests, and
though its period is shorter, it would still take years to exhaust on one core generating only random numbers.
LightRNG
is also very fast, but relative to XoRoRNG it has a significantly shorter period (the amount of
random numbers it will go through before repeating), at pow(2, 64)
as opposed to XoRoRNG's
pow(2, 128) - 1
, but LightRNG also allows the current RNG state to be retrieved and altered with
getState()
and setState()
. For most cases, you should decide between DiverRNG, LightRNG, XoRoRNG,
and other RandomnessSource implementations based on your needs for period length and state manipulation (DiverRNG
is also used internally by almost all StatefulRNG objects). You might want significantly less predictable random
results, which IsaacRNG
can provide, along with a large period. You may want a very long period of random
numbers, which would suggest LongPeriodRNG
as a good choice or MersenneTwister
as a potential
alternative. You may want better performance on 32-bit machines or on GWT, where Starfish32RNG
is currently
the best choice most of the time, and Lathe32RNG
can be faster but has slightly worse quality (both of these
generators use a 32-bit variant on the xoroshiro algorithm but change the output scrambler). These all can generate
pseudo-random numbers in a handful of nanoseconds (with the key exception of 64-bit generators being used on GWT,
where they may take more than 100 nanoseconds per number), so unless you need a LOT of random numbers in a hurry,
they'll probably all be fine on performance. You may want to decide on the special features of a generator, indicated
by implementing StatefulRandomness
if their state can be read and written to, and/or
SkippingRandomness
if sections in the generator's sequence can be skipped in long forward or backward leaps.
Constructor and Description |
---|
XoRoRNG()
Creates a new generator seeded using four calls to Math.random().
|
XoRoRNG(long seed)
Constructs this XoRoRNG by dispersing the bits of seed using
setSeed(long) across the two parts of state
this has. |
XoRoRNG(long stateA,
long stateB)
Constructs this XoRoRNG by calling
setSeed(long, long) on the arguments as given; see that method for
the specific details (stateA and stateB are kept as-is unless they are both 0). |
Modifier and Type | Method and Description |
---|---|
XoRoRNG |
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()
Gets the first component of this generator's two-part state, as a long.
|
long |
getStateB()
Gets the second component of this generator's two-part 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.
|
boolean |
nextBoolean() |
void |
nextBytes(byte[] bytes) |
double |
nextDouble() |
float |
nextFloat() |
int |
nextInt()
Can return any int, positive or negative, of any size permissible in a 32-bit signed integer.
|
int |
nextInt(int bound)
Exclusive on the outer bound; the inner bound is 0.
|
int |
nextInt(int inner,
int outer)
Inclusive lower, exclusive upper.
|
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 |
nextLong(long bound)
Exclusive on the outer bound; the inner bound is 0.
|
long |
nextLong(long inner,
long outer)
Inclusive inner, exclusive outer; both inner and outer can be positive or negative.
|
void |
setSeed(long seed)
Sets the seed of this generator using one long, running that through LightRNG's algorithm twice to get the state.
|
void |
setSeed(long stateA,
long stateB)
Sets the seed of this generator using two longs, using them without changes unless both are 0 (then it makes the
state variable corresponding to stateA 1 instead).
|
java.lang.String |
toString() |
public XoRoRNG()
public XoRoRNG(long seed)
setSeed(long)
across the two parts of state
this has.seed
- a long that won't be used exactly, but will affect both components of statepublic XoRoRNG(long stateA, long stateB)
setSeed(long, long)
on the arguments 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 long nextLong()
RandomnessSource
nextLong
in interface RandomnessSource
public XoRoRNG copy()
copy
in interface RandomnessSource
public int nextInt()
public int nextInt(int bound)
IRNG.nextSignedInt(int)
if you need a negative outer bound.bound
- the outer exclusive bound; should be positivepublic int nextInt(int inner, int outer)
inner
- the inner bound, inclusive, can be positive or negativeouter
- the outer bound, exclusive, should be positive, should usually be greater than innerpublic long nextLong(long bound)
bound
- the outer exclusive bound; may be positive or negativepublic long nextLong(long inner, long outer)
inner
- the inner bound, inclusive, can be positive or negativeouter
- the outer bound, exclusive, can be positive or negative and may be greater than or less than innerpublic double nextDouble()
public float nextFloat()
public boolean nextBoolean()
public void nextBytes(byte[] bytes)
public void setSeed(long seed)
seed
- the number to use as the seedpublic void setSeed(long stateA, long stateB)
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 long getStateA()
getStateB()
. You can set the state with two exact
values using setSeed(long, long)
, but the alternative overload setSeed(long)
won't use the
state without changing it (it needs to cover 128 bits with a 64-bit value).public long getStateB()
getStateA()
. You can set the state with two exact
values using setSeed(long, long)
, but the alternative overload setSeed(long)
won't use the
state without changing it (it needs to cover 128 bits with a 64-bit value).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.