Package squidpony.squidmath
Class NLFSR
java.lang.Object
squidpony.squidmath.NLFSR
- All Implemented Interfaces:
Serializable
,RandomnessSource
,StatefulRandomness
public class NLFSR extends Object implements StatefulRandomness, Serializable
A Non-Linear Feedback Shift Register that may be used like a StatefulRandomness but is not truly random. This is
based on the
LFSR
class, and is less predictable but is otherwise less than optimal in some ways. It has a
period of (2 to the 27) minus 1, and uses data from
http://people.kth.se/~dubrova/nlfsr.html and https://eprint.iacr.org/2012/314.pdf . You would normally only prefer
NLFSR over LFSR if you expect players to scrutinize your randomly-generated data, or if you want to use it as part of
a more complex process such as encoding a saved file in a more robust way. Since 2 to the 27 numbers can be produced
and analyzed in a matter of seconds, you'd need a lot of independent steps like this to actually improve encoding.
It is important to note that an NLFSR or LFSR will produce each number from 1 until its maximum exactly once before
repeating, so this may be useful as a way of generating test data in an unpredictable order.- Author:
- Tommy Ettinger
- See Also:
- Serialized Form
-
Field Summary
Fields Modifier and Type Field Description int
state
-
Constructor Summary
Constructors Constructor Description NLFSR()
Creates a new generator seeded using Math.random.NLFSR(int seed)
NLFSR(CharSequence seed)
-
Method Summary
Modifier and Type Method Description NLFSR
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(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()
Produces up to 27 bits of random int, with a minimum result of 1 and a max of 134217727 (both inclusive).int
nextInt(int bound)
Exclusive on the upper bound.int
nextInt(int lower, int upper)
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 upper bound.void
setState(long seed)
Sets the seed of this generator using one long, running that through LightRNG's algorithm twice to get the state.String
toString()
-
Field Details
-
Constructor Details
-
Method Details
-
next
Description copied from interface:RandomnessSource
Using this method, any algorithm that might use the built-in Java Random can interface with this randomness source.- Specified by:
next
in interfaceRandomnessSource
- Parameters:
bits
- the number of bits to be returned- Returns:
- the integer containing the appropriate number of bits
-
nextLong
Description copied from interface:RandomnessSource
Using this method, any algorithm that needs to efficiently generate more than 32 bits of random data can interface with this randomness source. Get a random long between Long.MIN_VALUE and Long.MAX_VALUE (both inclusive).- Specified by:
nextLong
in interfaceRandomnessSource
- Returns:
- a random long between Long.MIN_VALUE and Long.MAX_VALUE (both inclusive)
-
nextInt
Produces up to 27 bits of random int, with a minimum result of 1 and a max of 134217727 (both inclusive).- Returns:
- a random int between 1 and 134217727, both inclusive
-
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. This just needs to copy the state so it isn't shared, usually, and produce a new value with the same exact state.- Specified by:
copy
in interfaceRandomnessSource
- Specified by:
copy
in interfaceStatefulRandomness
- Returns:
- a copy of this RandomnessSource
-
nextInt
Exclusive on the upper bound. The lower bound is 0.- Parameters:
bound
- the upper bound; should be positive- Returns:
- a random int less than n and at least equal to 0
-
nextInt
Inclusive lower, exclusive upper.- Parameters:
lower
- the lower bound, inclusive, can be positive or negativeupper
- the upper bound, exclusive, should be positive, must be greater than lower- Returns:
- a random int at least equal to lower and less than upper
-
nextLong
Exclusive on the upper bound. The lower bound is 0.- Parameters:
bound
- the upper bound; should be positive- Returns:
- a random long less than n
-
nextDouble
-
nextFloat
-
nextBoolean
-
nextBytes
-
getState
Get the current internal state of the StatefulRandomness as a long.- Specified by:
getState
in interfaceStatefulRandomness
- Returns:
- the current internal state of this object.
-
setState
Sets the seed of this generator using one long, running that through LightRNG's algorithm twice to get the state.- Specified by:
setState
in interfaceStatefulRandomness
- Parameters:
seed
- the number to use as the seed
-
toString
-
equals
-
hashCode
-