public class TweakRNG extends AbstractRNG implements java.io.Serializable
nextLong()
.
TangleRNG
, which is a fairly solid, very fast algorithm, and uses its results two
at a time to give to an atan2 calculation (specifically, NumberTools.atan2_(float, float)
or
NumberTools.atan2_(double, double)
. These particular approximations of atan2 range from 0.0 to 1.0 instead of
-pi to pi. This means atan2 inputs with positive x and small y are likely to return values near 1.0 or near 0.0, but
not between 0.25 and 0.75. The opposite is true for inputs with negative x and small y; that is likely to be near 0.5
and can't be between 0.0 and 0.25 or between 0.75 and 1.0. TweakRNG uses this property to implement centrality,
changing the inputs to its internal atan2 usage so x is positive when centrality is positive, or negative when
centrality is negative. Likewise, favor is implemented by changing y, though reversed; positive favor makes the atan2
calculation adjusted with negative y, making it more likely to be between 0.5 and 1.0, while negative favor pushes it
back to between 0.0 and 0.5.
Constructor and Description |
---|
TweakRNG() |
TweakRNG(long seed) |
TweakRNG(long seedA,
long seedB,
long centrality) |
TweakRNG(long seedA,
long seedB,
long centrality,
long favor) |
Modifier and Type | Method and Description |
---|---|
TweakRNG |
copy()
Creates a copy of this IRNG; it will generate the same random numbers, given the same calls in order, as this
IRNG at the point copy() is called.
|
long |
getCentrality() |
long |
getFavor() |
long |
getStateA() |
long |
getStateB() |
int |
next(int bits)
Get up to 32 bits (inclusive) of random output; the int this produces
will not require more than
bits bits to represent. |
boolean |
nextBoolean()
Get a random bit of state, interpreted as true or false with approximately equal likelihood.
|
double |
nextDouble()
Gets a random double between 0.0 inclusive and 1.0 exclusive.
|
float |
nextFloat()
Gets a random float between 0.0f inclusive and 1.0f exclusive.
|
int |
nextInt()
Get a random integer between Integer.MIN_VALUE to Integer.MAX_VALUE (both inclusive).
|
long |
nextLong()
Get a random long between Long.MIN_VALUE to Long.MAX_VALUE (both inclusive).
|
void |
setCentrality(long centrality)
Adjusts the central bias of this TweakRNG, often to positive numbers (which bias toward the center of the range),
but also often to negative numbers (which bias toward extreme values, though still within the range).
|
void |
setFavor(long favor)
Adjusts the value bias of this TweakRNG, often to positive numbers (which bias toward higher results), but also
often to negative numbers (which bias toward lower results).
|
void |
setStateA(long stateA) |
void |
setStateB(long stateB) |
java.io.Serializable |
toSerializable()
Gets a view of this IRNG in a way that implements
Serializable , which may simply be this IRNG if it
implements Serializable as well as IRNG. |
between, between, between, getRandomElement, getRandomElement, getRandomElement, nextDouble, nextFloat, nextInt, nextLong, nextSignedInt, nextSignedLong, randomOrdering, randomOrdering, randomPortion, shuffle, shuffle, shuffle, shuffle, shuffleInPlace, shuffleInPlace, swap
public TweakRNG()
public TweakRNG(long seed)
public TweakRNG(long seedA, long seedB, long centrality)
public TweakRNG(long seedA, long seedB, long centrality, long favor)
public int next(int bits)
AbstractRNG
bits
bits to represent.next
in interface IRNG
next
in interface RandomnessSource
next
in class AbstractRNG
bits
- an int between 1 and 32, both inclusivepublic int nextInt()
AbstractRNG
nextInt
in interface IRNG
nextInt
in class AbstractRNG
public long nextLong()
AbstractRNG
nextLong
in interface IRNG
nextLong
in interface RandomnessSource
nextLong
in class AbstractRNG
public boolean nextBoolean()
AbstractRNG
AbstractRNG.next(int)
to get 1 bit,
returning next(1) == 1
, but others will get much better results with a sign check by calling their choice
of AbstractRNG.nextInt()
or AbstractRNG.nextLong()
and returning nextInt() < 0
or nextLong < 0L
. For
example, an implementation that uses a linear congruential generator without truncating some lower bits will have
very-low-period results for the bottom bit (alternating true and false), but perfectly fine results from a sign
check. There are tested issues on the bottom (at least 2) bits of XoRoRNG
, but again not on a sign check.nextBoolean
in interface IRNG
nextBoolean
in class AbstractRNG
public double nextDouble()
AbstractRNG
(nextLong() & 0x1fffffffffffffL) * 0x1p-53
, which is recommended if
longs are fast to produce.nextDouble
in interface IRNG
nextDouble
in class AbstractRNG
public float nextFloat()
AbstractRNG
(nextInt() & 0xffffff) * 0x1p-24f
,
(nextLong() & 0xffffffL) * 0x1p-24f
, or next(24) * 0x1p-24f
, any of which can work when the
method they call is high-quality and fast. You probably would want to use nextInt() or next() if your
implementation is natively 32-bit and is slower at producing longs, for example.nextFloat
in interface IRNG
nextFloat
in class AbstractRNG
public TweakRNG copy()
AbstractRNG
UnsupportedOperationException
or return a new
IRNG of the same type but with a random seed, with the latter meant as a partial defense against cheating.
copy
in interface RandomnessSource
copy
in class AbstractRNG
public java.io.Serializable toSerializable()
AbstractRNG
Serializable
, which may simply be this IRNG if it
implements Serializable as well as IRNG.
RNG
initialized by calling
RNG(long)
with AbstractRNG.nextLong()
if you are unable to save the current state of this IRNG and the
caller still needs something saved. This won't preserve the current state or the choice of IRNG implementation,
however, so it is simply a last resort in case you don't want to throw an exception.toSerializable
in interface IRNG
toSerializable
in class AbstractRNG
Serializable
view of this IRNG or a similar one; may be this
public long getStateA()
public void setStateA(long stateA)
public long getStateB()
public void setStateB(long stateB)
public long getCentrality()
public void setCentrality(long centrality)
centrality
- should be between -65535 and 65535; positive values bias toward the center of the rangepublic long getFavor()
public void setFavor(long favor)
nextLong()
; in that case, the values are treated as higher when unsigned, so positive favor
makes both high positive values and all negative values more common. Doubles and floats will behave normally.favor
- should be between -65535 and 65535; positive values bias toward higher (unsigned for longs) resultsCopyright © Eben Howard 2012–2022. All rights reserved.