Package squidpony.squidmath
Class IsaacRNG
java.lang.Object
squidpony.squidmath.IsaacRNG
- All Implemented Interfaces:
Serializable
,RandomnessSource
public class IsaacRNG extends Object implements RandomnessSource
This is a port of the public domain Isaac64 (cryptographic) random number generator to Java.
It is a RandomnessSource here, so it should generally be used to make an RNG, which has more
features. IsaacRNG is slower than the non-cryptographic RNGs in SquidLib, but much faster
than cryptographic RNGs that need SecureRandom, plus it's compatible with GWT and Android!
Created by Tommy Ettinger on 8/1/2016.
- See Also:
- Serialized Form
-
Constructor Summary
Constructors Constructor Description IsaacRNG()
Constructs an IsaacRNG with no seed; this will produce one sequence of numbers as if the seed were 0 (which it essentially is, though passing 0 to the constructor that takes a long will produce a different sequence) instead of what the other RandomnessSources do (initialize with a low-quality random number from Math.random()).IsaacRNG(long seed)
Constructs an IsaacRNG with its state filled by the value of seed, run through the LightRNG algorithm.IsaacRNG(long[] seed)
Constructs an IsaacRNG with the given seed, which should be a rather large array of long values.IsaacRNG(String seed)
Constructs an IsaacRNG with its state filled by repeated hashing of seed. -
Method Summary
Modifier and Type Method Description IsaacRNG
copy()
Produces another RandomnessSource, but the new one will not produce the same data as this one.boolean
equals(Object o)
void
fillBlock(long[] data)
Generates enough pseudo-random long values to filldata
and assigns them to it.void
init(boolean flag)
Initializes this IsaacRNG; typically used from the constructor but can be called externally.void
init(long seed)
Can be used to re-initialize this IsaacRNG as if using the single-long constructor.void
init(long[] seed)
Can be used to re-initialize this IsaacRNG as if using the long-array constructor.void
init(String seed)
Can be used to re-initialize this IsaacRNG as if using the single-String constructor.int
next(int bits)
Using this method, any algorithm that might use the built-in Java Random can interface with this randomness source.long[]
nextBlock()
Generates and returns a block of 256 pseudo-random long values.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.void
regen()
Generates 256 results to be used by later calls to next() or nextLong().String
toString()
-
Constructor Details
-
IsaacRNG
public IsaacRNG()Constructs an IsaacRNG with no seed; this will produce one sequence of numbers as if the seed were 0 (which it essentially is, though passing 0 to the constructor that takes a long will produce a different sequence) instead of what the other RandomnessSources do (initialize with a low-quality random number from Math.random()). -
IsaacRNG
Constructs an IsaacRNG with the given seed, which should be a rather large array of long values. You should try to make seed a long[256], but smaller arrays will be tolerated without error. Arrays larger than 256 items will only have the first 256 used.- Parameters:
seed
- an array of longs to use as a seed; ideally it should be 256 individual longs
-
IsaacRNG
Constructs an IsaacRNG with its state filled by the value of seed, run through the LightRNG algorithm.- Parameters:
seed
- any long; will have equal influence on all bits of state
-
IsaacRNG
Constructs an IsaacRNG with its state filled by repeated hashing of seed.- Parameters:
seed
- a String that should be exceptionally long to get the best results.
-
-
Method Details
-
regen
Generates 256 results to be used by later calls to next() or nextLong(). This is a fast (not small) implementation. -
init
Can be used to re-initialize this IsaacRNG as if using the long-array constructor. The given seed should be a rather large array of long values. You should try to make seed a long[256], but smaller arrays will be tolerated without error. Arrays larger than 256 items will only have the first 256 used.- Parameters:
seed
- an array of longs to use as a seed; ideally it should be 256 individual longs
-
init
Can be used to re-initialize this IsaacRNG as if using the single-long constructor.- Parameters:
seed
- any long; will have equal influence on all bits of state
-
init
Can be used to re-initialize this IsaacRNG as if using the single-String constructor.- Parameters:
seed
- a String; if non-null, its contents will be used as a seed
-
init
Initializes this IsaacRNG; typically used from the constructor but can be called externally.- Parameters:
flag
- if true, use data from seed; if false, initializes this to unseeded random state
-
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)
-
nextBlock
Generates and returns a block of 256 pseudo-random long values.- Returns:
- a freshly-allocated array of 256 pseudo-random longs, with all bits possible
-
fillBlock
Generates enough pseudo-random long values to filldata
and assigns them to it. -
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
-
copy
Produces another RandomnessSource, but the new one will not produce the same data as this one. This is meant to be a "more-secure" generator, so this helps reduce the ability to guess future results from a given sequence of output.- Specified by:
copy
in interfaceRandomnessSource
- Returns:
- another RandomnessSource with the same implementation but no guarantees as to generation
-
equals
-
toString
-