public class MoonwalkRNG extends AbstractRNG implements IStatefulRNG, SkippingRandomness, java.io.Serializable
inverseNextLong(long)
method,
and distance checks between two generated numbers with the static distance(long, long)
method. A task this
might be useful for could be simple obfuscation that is hard to undo unless you know the starting state, like this:
nextInt()
),skip(long)
,previousInt()
(such as a XOR if the number was originally modified
with a XOR or an addition if it was originally modified with a subtraction),StatefulRNG
if it always used LightRNG
and allowed access to LightRNG's
skip() method as well as the reverse lookup and distance methods that aren't in LightRNG but are allowed by it.
previousLong()
and skip(long)
for advancing backwards, but also inverseNextLong(long)
to
go from output back to state.
Constructor and Description |
---|
MoonwalkRNG()
Default constructor; uses a random seed.
|
MoonwalkRNG(java.lang.CharSequence seedString)
String-seeded constructor; uses a platform-independent hash of the String (it does not use String.hashCode) as a
seed for this RNG.
|
MoonwalkRNG(long seed)
Constructs a MoonwalkRNG with the given seed as-is; any seed can be given.
|
Modifier and Type | Method and Description |
---|---|
MoonwalkRNG |
copy()
Creates a copy of this MoonwalkRNG; it will generate the same random numbers, given the same calls in order, as
this MoonwalkRNG at the point copy() is called.
|
static long |
distance(long out1,
long out2)
Returns the number of steps (where a step is equal to one call to most random number methods in this class)
needed to go from receiving out1 from a MoonwalkRNG's
nextLong() method to receiving out2 from another
call. |
boolean |
equals(java.lang.Object o) |
long |
getState()
Get the current internal state of the StatefulRandomness as a long.
|
int |
hashCode() |
static long |
inverseNextLong(long out)
Given the output of a call to
nextLong() as out , this finds the state of the MoonwalkRNG that
produce that output. |
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).
|
int |
previousInt()
Get a random integer between Integer.MIN_VALUE to Integer.MAX_VALUE (both inclusive), but advances the state
"backwards," such that calling
nextInt() alternating with this method will return the same pair of
numbers for as long as you keep alternating those two calls. |
long |
previousLong()
Get a random long between Long.MIN_VALUE to Long.MAX_VALUE (both inclusive), but advances the state
"backwards," such that calling
nextLong() alternating with this method will return the same pair of
numbers for as long as you keep alternating those two calls. |
void |
setState(long state)
Set the current internal state of this StatefulRandomness with a long; all longs are allowed.
|
long |
skip(long advance)
Advances or rolls back the SkippingRandomness' state without actually generating each number.
|
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. |
java.lang.String |
toString() |
between, between, between, getRandomElement, getRandomElement, getRandomElement, nextDouble, nextFloat, nextInt, nextLong, nextSignedInt, nextSignedLong, randomOrdering, randomOrdering, randomPortion, shuffle, shuffle, shuffle, shuffle, shuffleInPlace, shuffleInPlace, swap
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
between, between, between, getRandomElement, getRandomElement, getRandomElement, nextDouble, nextFloat, nextInt, nextLong, nextSignedInt, nextSignedLong, randomOrdering, randomOrdering, randomPortion, shuffle, shuffle, shuffle, shuffle, shuffleInPlace, shuffleInPlace
public MoonwalkRNG()
public MoonwalkRNG(long seed)
seed
- any longpublic MoonwalkRNG(java.lang.CharSequence seedString)
seedString
- any CharSequence, such as a String or StringBuilder; if null this will use the seed 0public int next(int bits)
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()
nextInt
in interface IRNG
nextInt
in class AbstractRNG
public long nextLong()
nextLong
in interface IRNG
nextLong
in interface RandomnessSource
nextLong
in class AbstractRNG
public int previousInt()
nextInt()
alternating with this method will return the same pair of
numbers for as long as you keep alternating those two calls. This can be useful with skip(long)
when it
advances ahead by a large amount and you want to step backward to reverse another set of forward-advancing number
generations that had been done by other code.public long previousLong()
nextLong()
alternating with this method will return the same pair of
numbers for as long as you keep alternating those two calls. This can be useful with skip(long)
when it
advances ahead by a large amount and you want to step backward to reverse another set of forward-advancing number
generations that had been done by other code.public boolean nextBoolean()
nextBoolean
in interface IRNG
nextBoolean
in class AbstractRNG
public double nextDouble()
nextDouble
in interface IRNG
nextDouble
in class AbstractRNG
public float nextFloat()
nextFloat
in interface IRNG
nextFloat
in class AbstractRNG
public MoonwalkRNG copy()
copy
in interface RandomnessSource
copy
in interface StatefulRandomness
copy
in class AbstractRNG
public java.io.Serializable toSerializable()
Serializable
, which may simply be this IRNG if it
implements Serializable as well as IRNG.
RNG
initialized by calling
RNG(long)
with 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 skip(long advance)
nextLong()
,
and returns the random number produced at that step. Negative numbers can be used to step backward, or 0 can be
given to get the most-recently-generated long from nextLong()
.skip
in interface SkippingRandomness
advance
- Number of future generations to skip over; can be negative to backtrack, 0 gets the most-recently-generated numberadvance
numberspublic long getState()
getState
in interface StatefulRandomness
public void setState(long state)
setState
in interface StatefulRandomness
state
- a 64-bit long; this can be any long, even 0public 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 inverseNextLong(long out)
nextLong()
as out
, this finds the state of the MoonwalkRNG that
produce that output. If you set the state of a MoonwalkRNG with setState(long)
to the result of this
method and then call nextLong()
on it, you should get back out
.
nextLong()
, but both run in constant time. Some random number generators take more
than constant time to reverse, so one was chosen for this class that would still be efficient (LightRNG
).
AbstractRNG.nextLong(long)
method by any generator.out
- a long as produced by nextLong()
, without changespublic static long distance(long out1, long out2)
nextLong()
method to receiving out2 from another
call. This number can be used with skip(long)
to move a MoonwalkRNG forward or backward by the desired
distance.out1
- a long as produced by nextLong()
, without changesout2
- a long as produced by nextLong()
, without changesnextLong()
that would be required to go from producing out1 to producing
out2; can be positive or negative, and can be passed to skip(long)
Copyright © Eben Howard 2012–2022. All rights reserved.