Class XSTR
- java.lang.Object
-
- java.util.Random
-
- com.gtnewhorizon.structurelib.util.XSTR
-
- All Implemented Interfaces:
java.io.Serializable
public class XSTR extends java.util.RandomXSTR - Xorshift ThermiteRandom Modified by Bogdan-G 03.06.2016 version 0.0.4- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static XSTRXSTR_INSTANCE
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description XSTRclone()longgetSeed()Returns the current state of the seed, can be used to clone the objectintnext(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.shtmlbooleannextBoolean()voidnextBytes(byte[] bytes_arr)doublenextDouble()floatnextFloat()doublenextGaussian()intnextInt()intnextInt(int bound)Returns a pseudorandom, uniformly distributedintvalue between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.longnextLong()voidsetSeed(long seed)Sets the seed for this pseudo random number generator.
-
-
-
Field Detail
-
XSTR_INSTANCE
public static final XSTR XSTR_INSTANCE
-
-
Constructor Detail
-
XSTR
public XSTR()
Creates a new pseudo random number generator. The seed is initialized to the current time, as if bysetSeed(System.currentTimeMillis());.
-
XSTR
public XSTR(long seed)
Creates a new pseudo random number generator, starting with the specified seed, usingsetSeed(seed);.- Parameters:
seed- the initial seed
-
-
Method Detail
-
nextBoolean
public boolean nextBoolean()
- Overrides:
nextBooleanin classjava.util.Random
-
nextDouble
public double nextDouble()
- Overrides:
nextDoublein classjava.util.Random
-
getSeed
public long getSeed()
Returns the current state of the seed, can be used to clone the object- Returns:
- the current seed
-
setSeed
public void setSeed(long seed)
Sets the seed for this pseudo random number generator. As described above, two instances of the same random class, starting with the same seed, produce the same results, if the same methods are called.- Overrides:
setSeedin classjava.util.Random- Parameters:
seed- the new seed
-
clone
public XSTR clone()
- Overrides:
clonein classjava.lang.Object- Returns:
- Returns an XSRandom object with the same state as the original
-
next
public 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- Overrides:
nextin classjava.util.Random- Parameters:
nbits- will shift nbits bits- Returns:
- next seed
-
nextGaussian
public double nextGaussian()
- Overrides:
nextGaussianin classjava.util.Random
-
nextInt
public int nextInt(int bound)
Returns a pseudorandom, uniformly distributedintvalue between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. The general contract ofnextIntis that oneintvalue in the specified range is pseudorandomly generated and returned. Allboundpossibleintvalues are produced with (approximately) equal probability. The methodnextInt(int bound)is implemented by classRandomas 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
intvalues 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.
- Overrides:
nextIntin classjava.util.Random- Parameters:
bound- the upper bound (exclusive). Must be positive.- Returns:
- the next pseudorandom, uniformly distributed
intvalue between zero (inclusive) andbound(exclusive) from this random number generator's sequence - Throws:
java.lang.IllegalArgumentException- if bound is not positive- Since:
- 1.2
-
nextInt
public int nextInt()
- Overrides:
nextIntin classjava.util.Random
-
nextFloat
public float nextFloat()
- Overrides:
nextFloatin classjava.util.Random
-
nextLong
public long nextLong()
- Overrides:
nextLongin classjava.util.Random
-
nextBytes
public void nextBytes(byte[] bytes_arr)
- Overrides:
nextBytesin classjava.util.Random
-
-