public class NLFSR extends java.lang.Object implements StatefulRandomness, java.io.Serializable
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.Modifier and Type | Field and Description |
---|---|
int |
state |
Constructor and Description |
---|
NLFSR()
Creates a new generator seeded using Math.random.
|
NLFSR(java.lang.CharSequence seed) |
NLFSR(int seed) |
Modifier and Type | Method and 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(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()
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.
|
java.lang.String |
toString() |
public NLFSR()
public NLFSR(int seed)
public NLFSR(java.lang.CharSequence seed)
public int next(int bits)
RandomnessSource
next
in interface RandomnessSource
bits
- the number of bits to be returnedpublic long nextLong()
RandomnessSource
nextLong
in interface RandomnessSource
public int nextInt()
public NLFSR copy()
copy
in interface RandomnessSource
copy
in interface StatefulRandomness
public int nextInt(int bound)
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, should be positive, must be greater than lowerpublic long nextLong(long bound)
bound
- the upper bound; should be positivepublic 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
Copyright © Eben Howard 2012–2022. All rights reserved.