public class MizuchiRNG extends java.lang.Object implements StatefulRandomness, java.io.Serializable
LinnormRNG
but modified to allow any odd number as a stream,
instead of LinnormRNG's hardcoded stream of 1. Although some streams may have quality issues, the structure is based
on a linear congruential generator where the stream is the additive component, and in that context all odd numbers
are usually considered equally effective. Has 64 bits of state, 64 bits used to store a stream (which cannot be
changed after construction) and natively outputs 64 bits at a time. Changes its state with a basic linear
congruential generator (it is simply state = state * 3935559000370003845L + stream
). Starting with that LCG's
output, it xorshifts that output twice, multiplies by a very large negative long, then returns another xorshift. Like
LinnormRNG, the output of this simple function passes all 32TB of PractRand (for one stream, it had 3 anomalies, but
another had none, and none were ever significant or persistent), meaning its statistical quality is excellent. The
speed of this particular class isn't fully clear yet, but benchmarks performed under the heavy load of PractRand
testing happening at the same time appeared to show no significant difference between LinnormRNG and MizuchiRNG in
speed (which means it's tied for second place in its category, behind DiverRNG
).
nextLong()
. LinnormRNG
uses the same algorithm except for the number added
in the LCG state update; there this number is always 1, but here it can be any odd long. This means that any given
MizuchiRNG object has two long values stored in it instead of the one in a LinnormRNG, but it allows two MizuchiRNG
objects with different streams to produce different, probably-not-correlated sequences of results, even with the same
seed. This property may be useful for cases where an adversary is trying to predict results in some way, though using
different streams for this purpose isn't enough and should be coupled with truncation of a large part of output (see
PCG-Random's techniques for this).
Constructor and Description |
---|
MizuchiRNG()
Creates a new generator seeded using Math.random.
|
MizuchiRNG(long seed) |
MizuchiRNG(long seed,
long stream) |
MizuchiRNG(java.lang.String seed) |
Modifier and Type | Method and Description |
---|---|
MizuchiRNG |
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()
Gets the current state of this generator.
|
long |
getStream()
Gets the stream of this generator.
|
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()
Gets a random value, true or false.
|
void |
nextBytes(byte[] bytes)
Given a byte array as a parameter, this will fill the array with random bytes (modifying it
in-place).
|
double |
nextDouble()
Gets a uniform random double in the range [0.0,1.0)
|
double |
nextDouble(double outer)
Gets a uniform random double in the range [0.0,outer) given a positive parameter outer.
|
float |
nextFloat()
Gets a uniform random float in the range [0.0,1.0)
|
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.
|
int |
nextInt(int inner,
int outer)
Inclusive inner, exclusive outer.
|
long |
nextLong()
Can return any long, positive or negative, of any size permissible in a 64-bit signed integer.
|
long |
nextLong(long bound)
Exclusive on the upper bound.
|
long |
nextLong(long lower,
long upper)
Inclusive lower, exclusive upper.
|
void |
setState(long seed)
Sets the seed (also the current state) of this generator.
|
java.lang.String |
toString() |
public MizuchiRNG()
public MizuchiRNG(long seed)
public MizuchiRNG(long seed, long stream)
public MizuchiRNG(java.lang.String seed)
public int next(int bits)
RandomnessSource
next
in interface RandomnessSource
bits
- the number of bits to be returnedpublic long nextLong()
nextLong
in interface RandomnessSource
public MizuchiRNG copy()
copy
in interface RandomnessSource
copy
in interface StatefulRandomness
public int nextInt()
public int nextInt(int bound)
IRNG.nextSignedInt(int)
if you need a negative outer bound.bound
- the upper bound; should be positivepublic int nextInt(int inner, int outer)
inner
- the inner bound, inclusive, can be positive or negativeouter
- the outer bound, exclusive, can be positive or negative, should be greater than innerpublic long nextLong(long bound)
bound
- the upper bound; should be positive (if negative, this returns 0)public long nextLong(long lower, long upper)
lower
- the lower bound, inclusive, can be positive or negativeupper
- the upper bound, exclusive, should be positive, must be greater than lowerpublic double nextDouble()
public double nextDouble(double outer)
outer
- the exclusive outer bound, can be negativepublic float nextFloat()
public boolean nextBoolean()
public void nextBytes(byte[] bytes)
Math.ceil(bytes.length / 8.0)
times.bytes
- a byte array that will have its contents overwritten with random bytes.public void setState(long seed)
setState
in interface StatefulRandomness
seed
- the seed to use for this MizuchiRNG, as if it was constructed with this seed.public long getState()
getState
in interface StatefulRandomness
public long getStream()
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.