public class LightRNG extends java.lang.Object implements RandomnessSource, StatefulRandomness, SkippingRandomness, java.io.Serializable
Modifier and Type | Field and Description |
---|---|
long |
state |
Constructor and Description |
---|
LightRNG()
Creates a new generator seeded using Math.random.
|
LightRNG(long seed) |
Modifier and Type | Method and Description |
---|---|
int |
compatibleNext(int bits)
Gets a pseudo-random int with at most the specified count of bits; for example, if bits is 3 this can return any
int between 0 and 2 to the 3 (that is, 8), exclusive on the upper end.
|
int |
compatibleNextInt(int bound)
Like
compatibleNext(int) , but for compatibility with nextInt(int) . |
int |
compatibleNextInt(int lower,
int upper)
Inclusive lower, exclusive upper.
|
long |
compatibleNextLong(long bound)
Exclusive on the upper bound.
|
long |
compatibleNextLong(long lower,
long upper)
Inclusive lower, exclusive upper.
|
LightRNG |
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.
|
static long |
determine(int a,
int b) |
static long |
determine(long state) |
static int |
determineBounded(long state,
int bound) |
static double |
determineDouble(long state)
Returns a random double that is deterministic based on state; if state is the same on two calls to this, this
will return the same float.
|
static float |
determineFloat(long state)
Returns a random float that is deterministic based on state; if state is the same on two calls to this, this will
return the same float.
|
boolean |
equals(java.lang.Object o) |
long |
getState()
Gets the current state of this generator.
|
int |
hashCode() |
int |
next(int bits)
Gets a pseudo-random int with at most the specified count of bits; for example, if bits is 3 this can return any
int between 0 and 2 to the 3 (that is, 8), exclusive on the upper end.
|
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)
Returns a random non-negative integer between 0 (inclusive) and the given bound (exclusive),
or 0 if the bound is 0.
|
int |
nextInt(int lower,
int upper)
Inclusive lower, exclusive upper.
|
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 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 (which is also the current state).
|
void |
setState(long seed)
Sets the seed (also the current state) of this generator.
|
long |
skip(long advance)
Advances or rolls back the LightRNG's state without actually generating each number.
|
java.lang.String |
toString() |
public LightRNG()
public LightRNG(long seed)
public int next(int bits)
compatibleNext(int)
, but its use is discouraged; it's slightly slower
for no good reason.next
in interface RandomnessSource
bits
- the number of bits to be returned; if 0 or less, or if 32 or greater, can return any 32-bit intpublic int compatibleNext(int bits)
next(int)
before some things changed on March 8 2018.
Using this method is discouraged unless you need to reproduce values exactly; it's slightly slower for no good
reason. Calling next(32)
and compatibleNext(32)
should have identical results, but other values
for bits will probably be different.bits
- the number of bits to be returned; if 0 or less, or if 32 or greater, can return any 32-bit intpublic long nextLong()
nextLong
in interface RandomnessSource
public LightRNG 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.
Uses an aggressively optimized technique that has some bias, but mostly for values of
bound over 1 billion. This method uses the same technique as RNG.nextIntHasty(int)
,
and like that method will always advance state exactly once (equivalent to one call to
nextLong()
).
bound
- the outer bound (exclusive), should be positivepublic int compatibleNextInt(int bound)
compatibleNext(int)
, but for compatibility with nextInt(int)
.
Exclusive on the upper bound. The lower bound is 0.bound
- the upper bound; should be positivepublic int nextInt(int lower, int upper)
lower
- the lower bound, inclusive, can be positive or negativeupper
- the upper bound, exclusive, can be positive or negative, should be greater than lowerpublic int compatibleNextInt(int lower, int upper)
lower
- the lower bound, inclusive, can be positive or negativeupper
- the upper bound, exclusive, should be positive, must be greater than lowerpublic 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 long compatibleNextLong(long bound)
nextInt(int)
or nextLong(long)
, this
may sometimes advance the state more than once, depending on what numbers are produced internally and the bound.
nextLong(long)
is preferred because it is much faster and reliably advances the state only once. Because
this method uses rejection sampling, getting multiple random longs to "smooth the odds" when the bound is such
that it can't fairly distribute one random long across all possible outcomes, it may be more "fair" than
nextLong(long)
, though it could potentially consume more of the period faster if pathologically bad
bounds were used very often, and if enough of the period is gone then statistical flaws may become detectable.bound
- the upper bound; if this isn't positive, this method always returns 0public long compatibleNextLong(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 setSeed(long seed)
seed
- the seed to use for this LightRNG, as if it was constructed with this seed.public void setState(long seed)
setState
in interface StatefulRandomness
seed
- the seed to use for this LightRNG, as if it was constructed with this seed.public long getState()
getState
in interface StatefulRandomness
public long skip(long advance)
getState()
).skip
in interface SkippingRandomness
advance
- Number of future generations to skip over; can be negative to backtrack, 0 gets the most recent generated numberpublic 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
public static long determine(long state)
public static long determine(int a, int b)
public static int determineBounded(long state, int bound)
public static float determineFloat(long state)
determine(++state)
,
where the increment for state should be odd but otherwise doesn't really matter. This multiplies state by
0x9E3779B97F4A7C15L
within this method, so using a small increment won't be much different from using a
very large one, as long as it is odd. The period is 2 to the 64 if you increment or decrement by 1, but there are
only 2 to the 30 possible floats between 0 and 1.state
- a variable that should be different every time you want a different random result;
using determine(++state)
is recommended to go forwards or determine(--state)
to
generate numbers in reverse orderstate
public static double determineDouble(long state)
determine(++state)
, where the increment for state should be odd but otherwise doesn't really matter. This
multiplies state by 0x9E3779B97F4A7C15L
within this method, so using a small increment won't be much
different from using a very large one, as long as it is odd. The period is 2 to the 64 if you increment or
decrement by 1, but there are only 2 to the 62 possible doubles between 0 and 1.state
- a variable that should be different every time you want a different random result;
using determine(++state)
is recommended to go forwards or determine(--state)
to
generate numbers in reverse orderstate
Copyright © Eben Howard 2012–2022. All rights reserved.