public class XorShiftStarRandomGenerator extends AbstractRandomGenerator
The methods of this class returns the same sequences as those of
XorShiftStarRandom
, to which documentation we refer for more details, but this class is not a subclass of Random
,
which unfortunately suffers from a very poor design and is very heavy on memory.
If you need a large number of generators and do not need to be an instance
of Random
, or simply if you need a RandomGenerator
to use
with Commons Math,
you might be wanting this class instead of XorShiftStarRandom
.
XorShiftStarRandom
Constructor and Description |
---|
XorShiftStarRandomGenerator()
Creates a new generator seeded using
Util.randomSeed() . |
XorShiftStarRandomGenerator(long seed)
Creates a new generator using a given seed.
|
Modifier and Type | Method and Description |
---|---|
boolean |
nextBoolean() |
void |
nextBytes(byte[] bytes) |
double |
nextDouble() |
float |
nextFloat() |
int |
nextInt() |
int |
nextInt(int n)
Returns a pseudorandom, approximately uniformly distributed
int value
between 0 (inclusive) and the specified value (exclusive), drawn from
this random number generator's sequence. |
long |
nextLong() |
long |
nextLong(long n)
Returns a pseudorandom uniformly distributed
long value
between 0 (inclusive) and the specified value (exclusive), drawn from
this random number generator's sequence. |
void |
setSeed(long seed)
Sets the seed of this generator.
|
clear, nextGaussian, setSeed, setSeed
public XorShiftStarRandomGenerator()
Util.randomSeed()
.public XorShiftStarRandomGenerator(long seed)
seed
- a nonzero seed for the generator (if zero, the generator will be seeded with -1).public long nextLong()
nextLong
in interface RandomGenerator
nextLong
in class AbstractRandomGenerator
public int nextInt()
nextInt
in interface RandomGenerator
nextInt
in class AbstractRandomGenerator
public int nextInt(int n)
int
value
between 0 (inclusive) and the specified value (exclusive), drawn from
this random number generator's sequence.
The hedge “approximately” is due to the fact that to be always
faster than ThreadLocalRandom
we return
the lower 63 bits of nextLong()
modulo n
instead of using
Random
's fancy algorithm (which nextLong(long)
uses though).
This choice introduces a bias: the numbers from 0 to 263 mod n
are slightly more likely than the other ones. In the worst case, “more likely”
means 1.00000000023 times more likely, which is in practice undetectable. If for some reason you
need truly uniform generation, just use nextLong(long)
.
nextInt
in interface RandomGenerator
nextInt
in class AbstractRandomGenerator
n
- the positive bound on the random number to be returned.int
value between 0
(inclusive) and n
(exclusive).public long nextLong(long n)
long
value
between 0 (inclusive) and the specified value (exclusive), drawn from
this random number generator's sequence. The algorithm used to generate
the value guarantees that the result is uniform, provided that the
sequence of 64-bit values produced by this generator is.n
- the positive bound on the random number to be returned.long
value between 0
(inclusive) and n
(exclusive).public double nextDouble()
nextDouble
in interface RandomGenerator
nextDouble
in class AbstractRandomGenerator
public float nextFloat()
nextFloat
in interface RandomGenerator
nextFloat
in class AbstractRandomGenerator
public boolean nextBoolean()
nextBoolean
in interface RandomGenerator
nextBoolean
in class AbstractRandomGenerator
public void nextBytes(byte[] bytes)
nextBytes
in interface RandomGenerator
nextBytes
in class AbstractRandomGenerator
public void setSeed(long seed)
setSeed
in interface RandomGenerator
setSeed
in class AbstractRandomGenerator
seed
- a nonzero seed for the generator (if zero, the generator will be seeded with -1).