Class JMSLRandom

java.lang.Object
com.softsynth.jmsl.JMSLRandom

public class JMSLRandom
extends java.lang.Object
Random number class. Algorithm from Cooper/Clancy "Oh! Pascal!"
Author:
Phil Burk and Nick Didkovsky
  • Field Summary

    Fields
    Modifier and Type Field Description
    static int NUM_GAUSS_LOOPS
    The higher the number of loops, the better gauss() approximates bell curve (Central Limit Theorem Method).
  • Constructor Summary

    Constructors
    Constructor Description
    JMSLRandom()  
  • Method Summary

    Modifier and Type Method Description
    static double choose()  
    static double choose​(double high)  
    static double choose​(double low, double high)  
    static int choose​(int high)  
    static int choose​(int low, int high)  
    static double choosePlusMinus​(double range)
    return random double between +range and -range
    static int choosePlusMinus​(int range)
    return random integer between +range and -range, inclusive
    static double gauss​(double variance, double xmu)
    Deprecated.
    use gaussVar() instead.
    static double gaussSD​(double sigma, double xmu)
    Return a Gaussian distributed value using Central Limit Theorem Method.
    static double gaussVar​(double variance, double xmu)
    Return a Gaussian distributed value using Central Limit Theorem Method.
    static int getSeed()  
    static void main​(java.lang.String[] args)  
    static int nextSeed()  
    static int qa()  
    static void randomize()
    set random seed from time.
    static void setSeed​(int s)  

    Methods inherited from class java.lang.Object

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

    • NUM_GAUSS_LOOPS

      public static int NUM_GAUSS_LOOPS
      The higher the number of loops, the better gauss() approximates bell curve (Central Limit Theorem Method). Defaults to min recommended 20, see http://www.dspguru.com/howto/tech/wgn2.htm
  • Constructor Details

    • JMSLRandom

      public JMSLRandom()
  • Method Details

    • randomize

      public static void randomize()
      set random seed from time. Call getSeed() to store the random seed generated here
    • setSeed

      public static void setSeed​(int s)
    • getSeed

      public static int getSeed()
      Returns:
      current seed
    • nextSeed

      public static int nextSeed()
    • choose

      public static double choose()
      Returns:
      random float [0..1)
    • choose

      public static int choose​(int high)
      Returns:
      random int [0..high)
    • choose

      public static double choose​(double high)
      Returns:
      random double [0..high)
    • choose

      public static int choose​(int low, int high)
      Returns:
      random int [low..high)
    • choose

      public static double choose​(double low, double high)
      Returns:
      random double [low..high)
    • choosePlusMinus

      public static int choosePlusMinus​(int range)
      return random integer between +range and -range, inclusive
    • choosePlusMinus

      public static double choosePlusMinus​(double range)
      return random double between +range and -range
    • gauss

      public static double gauss​(double variance, double xmu)
      Deprecated.
      use gaussVar() instead.
      See Also:
      com.softsynth.jmsl.JMSLRandom.gaussSD, com.softsynth.jmsl.JMSLRandom.gaussVar
    • gaussVar

      public static double gaussVar​(double variance, double xmu)
      Return a Gaussian distributed value using Central Limit Theorem Method. variance (standard deviation squared) controls narrowness of bell, xmu (mean) controls the centering of bell. Small variance make narrow bells.

      See discussion of how to control width of bell in gaussSD() comments.

      Comments from Matt Donadio at http://www.dspguru.com/howto/tech/wgn2.htm, "The Central Limit Theorm states that the sum of N randoms will approach normal distribution as N approaches infinity. The drawback to this method is that X will be in the range [-N, N], instead of (-Infinity, Infinity) and if the calls to uniform are not truly independent, then the noise will no longer be white. Jeruchim, et. al., recommend N >=20 for good results."
      Parameters:
      variance - is Variance (standard deviation squared)
      xmu - is mean
    • gaussSD

      public static double gaussSD​(double sigma, double xmu)
      Return a Gaussian distributed value using Central Limit Theorem Method. sigma (standard deviation) controls narrowness of bell, xmu (mean) controls the centering of bell. Small sigmas make narrow bells. See comment below to understand how to use these values to get the desired range.

      The Empirical Rule states that About 68% of values drawn from a normal distribution are within one standard deviation (sigma) away from the mean (xmu); about 95% of the values are within two standard deviations and about 99.7% lie within three standard deviations. We can use this rule to inform which values we send to gaussSD()
      Example:
      You want a mean of 1.0 with the bell extending about 0.5 to the left and 0.5 to the right of the mean. That is you want most of your values to center around 1 and you want it very unlikely that values will stray outside the interval (0.5 .. 1.5). By the Empirical Rule we know 99.7% of the values will lie within 3 sigmas. Since the desired range is 0.5, divide that by 3 sigmas to get sigma = 0.5/3 = 0.166667. You would then call gaussSD(0.166667, 1.0) to get values in your desired range.

      Comments from Matt Donadio at http://www.dspguru.com/howto/tech/wgn2.htm, "The Central Limit Theorm states that the sum of N randoms will approach normal distribution as N approaches infinity. The drawback to this method is that X will be in the range [-N, N], instead of (-Infinity, Infinity) and if the calls to uniform are not truly independent, then the noise will no longer be white. Jeruchim, et. al., recommend N >=20 for good results."
      Parameters:
      sigma - is Standard Deviation (99.7% of gaussian generated values fall within 3 sigmas of mean)
      xmu - is mean, or center of the bell.
    • qa

      public static int qa()
    • main

      public static void main​(java.lang.String[] args)