public class FastNonThreadsafeRandom
extends java.util.Random
Random
, but not using atomic long
seeds. This implementation is no longer thread-safe (but faster)!
It is still the same Linear Congruential Generator (LCG), with a cycle length
of 248, of which we only use 32 bits at a time. Given the same
seed, it is expected to produce the exact same random sequence as Java's
Random
.
Modifier and Type | Field and Description |
---|---|
private static long |
addend |
protected static java.lang.String |
BADBOUND
Exception message for non-positive bounds
|
private static long |
mask |
private static long |
multiplier |
private long |
seed
The random seed.
|
private static long |
serialVersionUID
Serial version number.
|
Constructor and Description |
---|
FastNonThreadsafeRandom()
Constructor called only by localRandom.initialValue.
|
FastNonThreadsafeRandom(long seed)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
protected int |
next(int bits) |
double |
nextDouble() |
int |
nextInt() |
int |
nextInt(int n)
Returns a pseudorandom, uniformly distributed
int value between 0
(inclusive) and the specified value (exclusive), drawn from this random
number generator's sequence. |
int |
nextIntRefined(int n)
Returns a pseudorandom, uniformly distributed
int value between 0
(inclusive) and the specified value (exclusive), drawn from this random
number generator's sequence. |
void |
setSeed(long seed) |
private static final long serialVersionUID
private static final long multiplier
private static final long addend
private static final long mask
private long seed
protected static final java.lang.String BADBOUND
public FastNonThreadsafeRandom()
public FastNonThreadsafeRandom(long seed)
seed
- Random generator seed.public void setSeed(long seed)
setSeed
in class java.util.Random
protected int next(int bits)
next
in class java.util.Random
public int nextInt()
nextInt
in class java.util.Random
public double nextDouble()
nextDouble
in class java.util.Random
@Reference(authors="D. Lemire", title="Fast random shuffling", booktitle="Daniel Lemire\'s blog", url="http://lemire.me/blog/2016/06/30/fast-random-shuffling/", bibkey="blog/Lemire16") public int nextInt(int n)
int
value between 0
(inclusive) and the specified value (exclusive), drawn from this random
number generator's sequence. The general contract of nextInt
is
that one int
value in the specified range is pseudorandomly
generated and returned. All n
possible int
values are
produced with (approximately) equal probability.
In contrast to the Java version, we use an approach that tries to avoid divisions for performance. We will have a slightly worse distribution in this fast version (see the XorShift generators for higher quality with rejection sampling) discussed in:
D. Lemire
Fast random shuffling
http://lemire.me/blog/2016/06/30/fast-random-shuffling/
nextInt
in class java.util.Random
@Reference(authors="D. Lemire", title="Fast random shuffling", booktitle="Daniel Lemire\'s blog", url="http://lemire.me/blog/2016/06/30/fast-random-shuffling/", bibkey="blog/Lemire16") public int nextIntRefined(int n)
int
value between 0
(inclusive) and the specified value (exclusive), drawn from this random
number generator's sequence. The general contract of nextInt
is
that one int
value in the specified range is pseudorandomly
generated and returned. All n
possible int
values are
produced with (approximately) equal probability.
In contrast to the Java version, we use an approach that tries to avoid divisions for performance. In this method, we also employ rejection sampling (for marginal improvement) as discussed in:
D. Lemire
Fast random shuffling
http://lemire.me/blog/2016/06/30/fast-random-shuffling/
In our experiments, the difference was negligible, as the rejections are quite rare events at least for our use case.
Copyright © 2019 ELKI Development Team. License information.