Class FastRandom

java.lang.Object
net.paulem.arcana.math.FastRandom
All Implemented Interfaces:
Serializable

public class FastRandom extends Object implements Serializable
An RNG class that's faster and hopefully better than java.util.Random.

(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

See discussions e.g. ... ...

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
    Modifier and Type
    Field
    Description
    static final FastRandom
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Creates 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.
    protected
    FastRandom(long seed)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    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.
    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 FastRandom
     
    byte[]
    static void
    main(String[] args)
     
    protected int
    next(int bits)
     
    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).
    nextBitSet(int size, double p)
    Return a random BitSet with "size" bits, each having probability p of being true.
    boolean
    Return a random boolean, equally likely to be true or false.
    boolean
    nextBoolean(double p)
    Return a random boolean, with probability p of being true.
    void
    nextBytes(byte[] bytes)
     
    double
    Return a random double drawn from an Chi-squarted distribution with mean 1 and variance 2.
    double
    nextChiSq(int df)
    Return a random double drawn from an Chi-squared distribution with mean df and variance 2*df.
    double
    nextChiSq(int df, double lambda)
    Return a random double drawn from an Chi-squared distribution with mean df+lambda and variance 2*df.
    int
    nextDiscrete(double[] a)
    Draw a single sample from multinomial "a".
    int
    nextDiscrete(double[] a, double sum)
    draw a single sample from (unnormalized) multinomial "a", with normalizing factor "sum".
    double
     
    double
    Return a random double drawn from an Exponential distribution with mean 1 and variance 1.
    double
    nextExp(double beta)
    Return a random double drawn from an Exponential distribution with mean beta and variance beta^2.
    double
    nextExp(double beta, double lambda)
    Return a random double drawn from an Exponential distribution with mean beta+lambda and variance beta^2.
    float
     
    double
    Return a random double drawn from a Gamma distribution with mean 1.0 and variance 1.0.
    double
    nextGamma(double alpha)
    Return a random double drawn from a Gamma distribution with mean alpha and variance 1.0.
    double
    nextGamma(double alpha, double beta)
    Return a random double drawn from a Gamma distribution with mean alpha*beta and variance alpha*beta^2.
    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.
    double
    Return a random double drawn from a Gaussian distribution with mean 0 and variance 1.
    double
    nextGaussian(double m, double s2)
    Return a random double drawn from a Gaussian distribution with mean m and variance s2.
    int
     
    int
    nextInt(int n)
     
    long
     
    int
    Return nextPoisson(1).
    int
    nextPoisson(double lambda)
    Return random integer from Poission with parameter lambda.
    double
    Return a random double in the range 0 to 1, inclusive, uniformly sampled from that range.
    double
    nextUniform(double a, double b)
    Return a random double in the range a to b, inclusive, uniformly sampled from that range.
    static FastRandom
    of()
     
    double
    oldNextGamma(int ia)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • INSTANCE

      public static final FastRandom 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

      public static FastRandom of()
    • getJ17FastRandom

      public static FastRandom 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

      public static void main(String[] args)
    • 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

      public BitSet nextBitSet(int size, double p)
      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()