Class FastRandom
- All Implemented Interfaces:
Serializable
(1) Is NOT synchronized! Thus NOT thread-safe. Therefore faster. In most applications I can think of, every thread should get its own RNG. Threadsafe can be nice for convenient utility functions; use java.util.Random for that. (2) Uses the XORShift RNG algorithm, which is faster and more accurate (more random) than the algorithm in java.util.Random. (3) Folds in the high-level methods from Mallet's cc.mallet.util.Randoms (4) Leaves out some of the convenience methods that appear in java.util.Random but not Mallet.
XORShift implementation from ... Mallet methods from ... ... i have no idea what license status that leaves this file
Speed tests, compared to java.util.Random - see main(): - 10.7 times faster for nextUniform() - 1.8 times faster for nextInt(1000) - I swapped this into an LDA collapsed Gibbs sampler, and got a 5% speedup of the entire program
Looking for feedback / bug reports.
- See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCreates a new RNG and seeds it using the default seeding strategy.FastRandom(byte[] seed) Creates an RNG and seeds it with the specified seed data.protectedFastRandom(long seed) -
Method Summary
Modifier and TypeMethodDescriptionstatic intconvertBytesToInt(byte[] bytes, int offset) Take four bytes from the specified position in the specified block and convert them into a 32-bit int, using the big-endian convention.static int[]convertBytesToInts(byte[] bytes) Convert an array of bytes into an array of ints. 4 bytes from the input data map to a single int in the output data.static FastRandombyte[]getSeed()static voidprotected intnext(int bits) doublenextBeta(double alpha, double beta) Return a random double drawn from a Beta distribution with mean a/(a+b) and variance ab/((a+b+1)(a+b)^2).nextBitSet(int size, double p) Return a random BitSet with "size" bits, each having probability p of being true.booleanReturn a random boolean, equally likely to be true or false.booleannextBoolean(double p) Return a random boolean, with probability p of being true.voidnextBytes(byte[] bytes) doubleReturn a random double drawn from an Chi-squarted distribution with mean 1 and variance 2.doublenextChiSq(int df) Return a random double drawn from an Chi-squared distribution with mean df and variance 2*df.doublenextChiSq(int df, double lambda) Return a random double drawn from an Chi-squared distribution with mean df+lambda and variance 2*df.intnextDiscrete(double[] a) Draw a single sample from multinomial "a".intnextDiscrete(double[] a, double sum) draw a single sample from (unnormalized) multinomial "a", with normalizing factor "sum".doubledoublenextExp()Return a random double drawn from an Exponential distribution with mean 1 and variance 1.doublenextExp(double beta) Return a random double drawn from an Exponential distribution with mean beta and variance beta^2.doublenextExp(double beta, double lambda) Return a random double drawn from an Exponential distribution with mean beta+lambda and variance beta^2.floatdoubleReturn a random double drawn from a Gamma distribution with mean 1.0 and variance 1.0.doublenextGamma(double alpha) Return a random double drawn from a Gamma distribution with mean alpha and variance 1.0.doublenextGamma(double alpha, double beta) Return a random double drawn from a Gamma distribution with mean alpha*beta and variance alpha*beta^2.doublenextGamma(double alpha, double beta, double lambda) Return a random double drawn from a Gamma distribution with mean alpha*beta+lamba and variance alpha*beta^2.doubleReturn a random double drawn from a Gaussian distribution with mean 0 and variance 1.doublenextGaussian(double m, double s2) Return a random double drawn from a Gaussian distribution with mean m and variance s2.intnextInt()intnextInt(int n) longnextLong()intReturn nextPoisson(1).intnextPoisson(double lambda) Return random integer from Poission with parameter lambda.doubleReturn a random double in the range 0 to 1, inclusive, uniformly sampled from that range.doublenextUniform(double a, double b) Return a random double in the range a to b, inclusive, uniformly sampled from that range.static FastRandomof()doubleoldNextGamma(int ia)
-
Field Details
-
INSTANCE
-
-
Constructor Details
-
FastRandom
protected FastRandom()Creates a new RNG and seeds it using the default seeding strategy. -
FastRandom
protected FastRandom(long seed) -
FastRandom
public FastRandom(byte[] seed) Creates an RNG and seeds it with the specified seed data.- Parameters:
seed- The seed data used to initialise the RNG.
-
-
Method Details
-
of
-
getJ17FastRandom
-
convertBytesToInt
public static int convertBytesToInt(byte[] bytes, int offset) Take four bytes from the specified position in the specified block and convert them into a 32-bit int, using the big-endian convention.- Parameters:
bytes- The data to read from.offset- The position to start reading the 4-byte int from.- Returns:
- The 32-bit integer represented by the four bytes.
-
convertBytesToInts
public static int[] convertBytesToInts(byte[] bytes) Convert an array of bytes into an array of ints. 4 bytes from the input data map to a single int in the output data.- Parameters:
bytes- The data to read from.- Returns:
- An array of 32-bit integers constructed from the data.
- Since:
- 1.1
-
main
-
getSeed
public byte[] getSeed() -
next
protected int next(int bits) -
nextInt
public int nextInt(int n) -
nextPoisson
public int nextPoisson(double lambda) Return random integer from Poission with parameter lambda. The mean of this distribution is lambda. The variance is lambda. -
nextPoisson
public int nextPoisson()Return nextPoisson(1). -
nextBoolean
public boolean nextBoolean()Return a random boolean, equally likely to be true or false. -
nextBoolean
public boolean nextBoolean(double p) Return a random boolean, with probability p of being true. -
nextBitSet
Return a random BitSet with "size" bits, each having probability p of being true. -
nextUniform
public double nextUniform()Return a random double in the range 0 to 1, inclusive, uniformly sampled from that range. The mean of this distribution is 0.5. The variance is 1/12. -
nextUniform
public double nextUniform(double a, double b) Return a random double in the range a to b, inclusive, uniformly sampled from that range. The mean of this distribution is (b-a)/2. The variance is (b-a)^2/12 -
nextDiscrete
public int nextDiscrete(double[] a) Draw a single sample from multinomial "a". -
nextDiscrete
public int nextDiscrete(double[] a, double sum) draw a single sample from (unnormalized) multinomial "a", with normalizing factor "sum". -
nextGaussian
public double nextGaussian()Return a random double drawn from a Gaussian distribution with mean 0 and variance 1. -
nextGaussian
public double nextGaussian(double m, double s2) Return a random double drawn from a Gaussian distribution with mean m and variance s2. -
nextGamma
public double nextGamma()Return a random double drawn from a Gamma distribution with mean 1.0 and variance 1.0. -
nextGamma
public double nextGamma(double alpha) Return a random double drawn from a Gamma distribution with mean alpha and variance 1.0. -
oldNextGamma
public double oldNextGamma(int ia) -
nextGamma
public double nextGamma(double alpha, double beta) Return a random double drawn from a Gamma distribution with mean alpha*beta and variance alpha*beta^2. -
nextGamma
public double nextGamma(double alpha, double beta, double lambda) Return a random double drawn from a Gamma distribution with mean alpha*beta+lamba and variance alpha*beta^2. Note that this means the pdf is:frac{ x^{alpha-1} exp(-x/beta) }{ beta^alpha Gamma(alpha) }in other words, beta is a "scale" parameter. An alternative parameterization would use 1/beta, the "rate" parameter. -
nextExp
public double nextExp()Return a random double drawn from an Exponential distribution with mean 1 and variance 1. -
nextExp
public double nextExp(double beta) Return a random double drawn from an Exponential distribution with mean beta and variance beta^2. -
nextExp
public double nextExp(double beta, double lambda) Return a random double drawn from an Exponential distribution with mean beta+lambda and variance beta^2. -
nextChiSq
public double nextChiSq()Return a random double drawn from an Chi-squarted distribution with mean 1 and variance 2. Equivalent to nextChiSq(1) -
nextChiSq
public double nextChiSq(int df) Return a random double drawn from an Chi-squared distribution with mean df and variance 2*df. -
nextChiSq
public double nextChiSq(int df, double lambda) Return a random double drawn from an Chi-squared distribution with mean df+lambda and variance 2*df. -
nextBeta
public double nextBeta(double alpha, double beta) Return a random double drawn from a Beta distribution with mean a/(a+b) and variance ab/((a+b+1)(a+b)^2). -
nextInt
public int nextInt() -
nextLong
public long nextLong() -
nextBytes
public void nextBytes(byte[] bytes) -
nextDouble
public double nextDouble() -
nextFloat
public float nextFloat()
-