public class XSTR extends Random
| Modifier and Type | Field and Description |
|---|---|
static XSTR |
XSTR_INSTANCE |
| Constructor and Description |
|---|
XSTR()
Creates a new pseudo random number generator.
|
XSTR(long seed)
Creates a new pseudo random number generator, starting with the specified seed, using
setSeed(seed);. |
| Modifier and Type | Method and Description |
|---|---|
XSTR |
clone() |
long |
getSeed()
Returns the current state of the seed, can be used to clone the object
|
int |
next(int nbits)
Implementation of George Marsaglia's elegant Xorshift random generator 30% faster and better quality than the
built-in java.util.random see also see http://www.javamex.com/tutorials/random_numbers/xorshift.shtml
|
boolean |
nextBoolean() |
void |
nextBytes(byte[] bytes_arr) |
double |
nextDouble() |
float |
nextFloat() |
double |
nextGaussian() |
int |
nextInt() |
int |
nextInt(int bound)
Returns a pseudorandom, uniformly distributed
int value between 0 (inclusive) and the specified value
(exclusive), drawn from this random number generator's sequence. |
long |
nextLong() |
void |
setSeed(long seed)
Sets the seed for this pseudo random number generator.
|
public static final XSTR XSTR_INSTANCE
public XSTR()
setSeed(System.currentTimeMillis());.public XSTR(long seed)
setSeed(seed);.seed - the initial seedpublic boolean nextBoolean()
nextBoolean in class Randompublic double nextDouble()
nextDouble in class Randompublic long getSeed()
public void setSeed(long seed)
public XSTR clone()
public int next(int nbits)
public double nextGaussian()
nextGaussian in class Randompublic int nextInt(int bound)
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 bound possible
int values are produced with (approximately) equal probability. The method nextInt(int bound) is
implemented by class Random as if by:
public int nextInt(int bound) {
if (bound <= 0)
throw new IllegalArgumentException("bound must be positive");
if ((bound & -bound) == bound) // i.e., bound is a power of 2
return (int)((bound * (long)next(31)) >> 31);
int bits, val;
do {
bits = next(31);
val = bits % bound;
} while (bits - val + (bound-1) < 0);
return val;
}
The hedge "approx imately" is used in the foregoing description only because the next method is only
approximately an unbiased source of independently chosen bits. If it were a perfect source of randomly chosen
bits, then the algorithm shown would choose int values from the stated range with perfect uniformity.
The algorithm is slightly tricky. It rejects values that would result in an uneven distribution (due to the fact that 2^31 is not divisible by n). The probability of a value being rejected depends on n. The worst case is n=2^30+1, for which the probability of a reject is 1/2, and the expected number of iterations before the loop terminates is 2.
The algorithm treats the case where n is a power of two specially: it returns the correct number of high-order bits from the underlying pseudo-random number generator. In the absence of special treatment, the correct number of low-order bits would be returned. Linear congruential pseudo-random number generators such as the one implemented by this class are known to have short periods in the sequence of values of their low-order bits. Thus, this special case greatly increases the length of the sequence of values returned by successive calls to this method if n is a small power of two.
nextInt in class Randombound - the upper bound (exclusive). Must be positive.int value between zero (inclusive) and bound
(exclusive) from this random number generator's sequenceIllegalArgumentException - if bound is not positive