public class PintRNG extends java.lang.Object implements RandomnessSource, StatefulRandomness, java.io.Serializable
Starfish32RNG
or its wrapper GWTRNG
if you need a
StatefulRandomness, Lathe32RNG
if you want optimal speed and don't mind distribution flaws when producing
longs, or Oriole32RNG
or XoshiroStarPhi32RNG
if you need a higher period but don't need
StatefulRandomness' state adjustment methods.
Modifier and Type | Field and Description |
---|---|
int |
state |
Constructor and Description |
---|
PintRNG()
Creates a new generator seeded using Math.random.
|
PintRNG(int a) |
PintRNG(long seed) |
Modifier and Type | Method and Description |
---|---|
PintRNG |
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 int |
determine(int state)
Gets a pseudo-random int that is a permutation of
state , which is an int. |
static int |
determine(int a,
int b) |
static int |
determineBounded(int state,
int bound) |
static int |
disperse(int state)
Like
determine(int) , gets a pseudo-random int that is a permutation of state , which is an int. |
static int |
disperseBounded(int state,
int bound) |
boolean |
equals(java.lang.Object o) |
long |
getState()
Gets the current state of this generator.
|
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()
Gets a random value, true or false.
|
void |
nextBytes(byte[] bytes)
Given a byte array as a parameter, this will fill the array with random bytes (modifying it
in-place).
|
double |
nextDouble()
Gets a uniform random double in the range [0.0,1.0)
|
double |
nextDouble(double outer)
Gets a uniform random double in the range [0.0,outer) given a positive parameter outer.
|
float |
nextFloat()
Gets a uniform random float in the range [0.0,1.0)
|
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 upper bound.
|
int |
nextInt(int lower,
int upper)
Inclusive lower, exclusive upper.
|
long |
nextLong()
Can return any long, positive or negative, of any size permissible in a 64-bit signed integer.
|
void |
setState(long seed)
Sets the current state of this generator (an int) using only the least-significant 32 bits of seed (by casting
a mask of those bits in seed to int, which helps ensure that a full 32 bits of state are possible).
|
int |
skip(int advance)
Advances or rolls back the PintRNG's state without actually generating each number.
|
java.lang.String |
toString() |
public PintRNG()
public PintRNG(long seed)
public PintRNG(int a)
public int next(int bits)
RandomnessSource
next
in interface RandomnessSource
bits
- the number of bits to be returnedpublic int nextInt()
public long nextLong()
nextLong
in interface RandomnessSource
public PintRNG copy()
copy
in interface RandomnessSource
copy
in interface StatefulRandomness
public int nextInt(int bound)
IRNG.nextSignedInt(int)
if you need a negative outer 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 double nextDouble()
public double nextDouble(double outer)
outer
- the exclusive outer bound, can be negativepublic float nextFloat()
public boolean nextBoolean()
public void nextBytes(byte[] bytes)
Math.ceil(bytes.length / 4.0)
times.bytes
- a byte array that will have its contents overwritten with random bytes.public void setState(long seed)
setState
in interface StatefulRandomness
seed
- the seed to use for this PintRNG, as if it was constructed with this seed.public long getState()
getState
in interface StatefulRandomness
public 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 int skip(int advance)
getState()
).
LightRNG
, but less time than calling nextInt()
advance
times. Skipping backwards by one step is the worst case for this.advance
- Number of future generations to skip past. Can be negative to backtrack.public static int determine(int state)
state
, which is an int.
This should normally be called with a technique like
PintRNG.determine(state = state * 0x2C9277B5 + 0xAC564B05)
, where 0xAC564B05 can be changed to any odd
constant as long as it is the same across calls to this. You can effectively produce multiple uncorrelated
streams by adding different constants in place of 0xAC564B05, applied to different states.state
- any int, but should be updated like state = state * 0x2C9277B5 + 0xAC564B05
, where 0xAC564B05 can be any odd-number constantpublic static int disperse(int state)
determine(int)
, gets a pseudo-random int that is a permutation of state
, which is an int.
Unlike determine(), this static method performs an extra step to avoid correlation between similar inputs, such
as 4, 5, and 6, and their outputs. If you already give very distant numbers as subsequent inputs to determine(),
then you should continue to use that method unless you discover issues with correlation; otherwise it's not a bad
idea to default to this method, though it is somewhat slower than determine(). This method is safe to use with
sequential ints, so you can call it with the technique PintRNG.disperse(++state)
, or just use it on int
data as you obtain it to randomize its values.state
- any intpublic static int determine(int a, int b)
public static int determineBounded(int state, int bound)
public static int disperseBounded(int state, int bound)
Copyright © Eben Howard 2012–2022. All rights reserved.