public class LFSR extends java.lang.Object implements StatefulRandomness, java.io.Serializable
Modifier and Type | Field and Description |
---|---|
long |
state |
Constructor and Description |
---|
LFSR()
Creates a new generator seeded using Math.random.
|
LFSR(java.lang.CharSequence seed) |
LFSR(long seed) |
Modifier and Type | Method and Description |
---|---|
LFSR |
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(long state)
Gets the next number that an LFSR would produce using
nextLong() if its state was state . |
static int |
determineBounded(int state,
int bound)
Gets an int using
determineInt(int) and bounds it to fit between 0 (inclusive) and bound (exclusive). |
static int |
determineBounded(long state,
int bound)
Gets the next number that an LFSR would produce using
nextInt(int) if its state was state and
bound was passed to nextInt(). |
static int |
determineByte(byte state)
Gets the next number from 1 to 255 that a different kind of LFSR would produce if its state was
state . |
static int |
determineInt(int state)
Gets the next number that a different kind of 32-bit LFSR would produce if its state was
state . |
static int |
determineIntSymmetrical(int state)
Gets the next int that a different kind of LFSR would produce if its state was
state . |
static int |
determinePositiveInt(int state)
Gets the next positive int that a different kind of 31-bit LFSR would produce if its state was
state . |
static long |
determinePositiveLong(long state)
Gets the next positive long that a different kind of 63-bit LFSR would produce if its state was
state . |
boolean |
equals(java.lang.Object o) |
long |
getState()
Get the current internal state of the StatefulRandomness 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 |
setState(long seed)
Sets the seed of this generator using one long, running that through LightRNG's algorithm twice to get the state.
|
java.lang.String |
toString() |
public LFSR()
public LFSR(long seed)
public LFSR(java.lang.CharSequence seed)
public final int next(int bits)
RandomnessSource
next
in interface RandomnessSource
bits
- the number of bits to be returnedpublic final long nextLong()
RandomnessSource
nextLong
in interface RandomnessSource
public LFSR 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 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 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 long getState()
getState
in interface StatefulRandomness
public void setState(long seed)
setState
in interface StatefulRandomness
seed
- the number to use as the seedpublic 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)
nextLong()
if its state was state
.
Does not allow state to be 0. Strongly consider using the result of this and assigning it to state if you expect
to call this again, such as with (state = LFSR.determine(state))
, which will ensure the long-term
properties of an LFSR hold up (such as having a period of ((2 to the 64) minus 1), or the guarantee that every
number from 1 to ((2 to the 64) minus 1), inclusive on both, will be generated once per period).state
- any long other than 0public static int determineByte(byte state)
state
.
Does not allow state to be 0. If given all byte values except 0 as arguments, will produce all ints 1-255.
Strongly consider using the result of this and assigning it to state if you expect to call this again, such as
with (state = LFSR.determineByte(state))
, which will ensure the long-term properties of an LFSR hold up
(such as having a period of 255, or the guarantee that every number from 1 to 255, inclusive on both, will be
generated once per period).state
- any byte other than 0public static int determineInt(int state)
state
.
Does not allow state to be 0. If given all int values except 0 as arguments, will produce all ints except 0.
Strongly consider using the result of this and assigning it to state if you expect to call this again, such as
with (state = LFSR.determineInt(state))
, which will ensure the long-term properties of an LFSR hold up
(such as having a period of ((2 to the 32) minus 1), or the guarantee that every number from 1 to ((2 to the 32)
minus 1), inclusive on both, will be generated once per period).state
- any long other than 0public static long determinePositiveLong(long state)
state
.
Does not allow state to be 0 or negative. If given all positive long values (not including 0) as arguments, will
produce all longs greater than 0. Strongly consider using the result of this and assigning it to state if you
expect to call this again, such as with (state = LFSR.determinePositiveLong(state))
, which will ensure
the long-term properties of an LFSR hold up (such as having a period of ((2 to the 63) minus 1), or the guarantee
that every number from 1 to ((2 to the 63) minus 1), inclusive on both, will be generated once per period).state
- any positive long, not including 0public static int determinePositiveInt(int state)
state
.
Does not allow state to be 0 or negative. If given all positive int values (not including 0) as arguments, will
produce all ints greater than 0. Strongly consider using the result of this and assigning it to state if you
expect to call this again, such as with (state = LFSR.determinePositiveInt(state))
, which will ensure the
long-term properties of an LFSR hold up (such as having a period of ((2 to the 31) minus 1), or the guarantee
that every number from 1 to ((2 to the 31) minus 1), inclusive on both, will be generated once per period).
state
- any positive int, not including 0public static int determineIntSymmetrical(int state)
state
.
Does not allow state to be Integer.MIN_VALUE
, nor will this produce a result of Integer.MIN_VALUE
(as long as Integer.MIN_VALUE
was not the input). If given all int values except
Integer.MIN_VALUE
as arguments, will produce all ints in the range [-2147483647,2147483647]
,
including 0 but not -2147483648 (the minimum int). Strongly consider using the result of this and assigning it to
state if you expect to call this again, such as with (state = LFSR.determineIntSymmetrical(state))
, which
will ensure the long-term properties of an LFSR hold up (such as having a period of ((2 to the 32) minus 1), or
the guarantee that every int except Integer.MIN_VALUE
will be generated once per period).
state
- any int other than -2147483648 (0x80000000), which is Integer.MIN_VALUE
; can produce 0public static int determineBounded(long state, int bound)
nextInt(int)
if its state was state
and
bound
was passed to nextInt(). Does not allow state to be 0, but bound can be negative, which causes this
not to produce positive numbers. This method is very predictable and its use is not encouraged; prefer using
determineBounded(int, int)
.state
- any long other than 0bound
- the exclusive bound on the result as an int; does better if the bound is not too high (below 10000?)determine(long)
would produce with the given state, but limited to bound; can return 0public static int determineBounded(int state, int bound)
determineInt(int)
and bounds it to fit between 0 (inclusive) and bound (exclusive).
Does not allow state to be 0, but bound can be negative, which causes this not to produce positive numbers.state
- any int other than 0bound
- the exclusive bound on the result as an int; does better if the bound is not too high (below 10000?)determineInt(int)
would produce with the given state, but limited to bound; can return 0Copyright © Eben Howard 2012–2022. All rights reserved.