Class RandomHelper


  • public class RandomHelper
    extends Object
    A helper class for creating random number streams and adding them to a RandomRegistry. Working with random numbers consists of two parts: the actual random number generator and the distributions (e.g. Uniform) take the numbers from the generator and return the appropriate values.

    The RandomHelper provides access to default distributions that are created from a default generator. Non-default distributions that use their own random generator or share the default one can also be created. All the create* and get* methods where a name does NOT have to be provided operate on the default generator and distributions. A default uniform distributions is created automatically and the nextInt* and nextDouble* methods will use this distribution.

    RandomHelper creates a default random number generator using the current timestamp as the seed. If you do not explicitly set your own seed, the distributions created via the create* calls will use this default generator. However, when the seed is set or reset, all the distributions, both default and non-, are invalidated, and a new random number generator is created as is a new default uniform distribution.

    All the distributions in RandomHelper are from the colt library. The return types of the get* and create* methods return these colt objects. See the colt library documentation at http://dsd.lbl.gov/~hoschek/colt/.

    • Constructor Summary

      Constructors 
      Constructor Description
      RandomHelper()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static cern.jet.random.Beta createBeta​(double alpha, double beta)
      Creates the default beta distribution using the default random number generator.
      static cern.jet.random.Binomial createBinomial​(int n, double p)
      Creates the default binomial distribution using the default random number generator.
      static cern.jet.random.BreitWigner createBreitWigner​(double mean, double gamma, double cut)
      Creates the default Breit Wigner distribution using the default random number generator.
      static cern.jet.random.BreitWignerMeanSquare createBreitWignerMeanSquare​(double mean, double gamma, double cut)
      Creates the default Breit Wigner mean square state distribution using the default random number generator.
      static cern.jet.random.ChiSquare createChiSquare​(double freedom)
      Creates the default chi square distribution using the default random number generator.
      static cern.jet.random.Empirical createEmpirical​(double[] pdf, int interpolationType)
      Creates the default empirical distribution using the default random number generator.
      static cern.jet.random.EmpiricalWalker createEmpiricalWalker​(double[] pdf, int interpolationType)
      Creates the default empirical walker distribution using the default random number generator.
      static cern.jet.random.Exponential createExponential​(double lambda)
      Creates the default exponential distribution using the default random number generator.
      static cern.jet.random.ExponentialPower createExponentialPower​(double tau)
      Creates the default exponential power distribution using the default random number generator.
      static cern.jet.random.Gamma createGamma​(double alpha, double lambda)
      Creates the default gamma distribution using the default random number generator.
      static cern.jet.random.Hyperbolic createHyperbolic​(double alpha, double beta)
      Creates the default hyperbolic distribution using the default random number generator.
      static cern.jet.random.HyperGeometric createHyperGeometric​(int N, int s, int n)
      Creates the default hyper geometric distribution using the default random number generator.
      static cern.jet.random.Logarithmic createLogarithmic​(double p)
      Creates the default logarithmic distribution using the default random number generator.
      static cern.jet.random.NegativeBinomial createNegativeBinomial​(int n, double p)
      Creates the default negative binomial distribution using the default random number generator.
      static cern.jet.random.Normal createNormal​(double mean, double standardDeviation)
      Creates the default normal distribution using the default random number generator.
      static cern.jet.random.Poisson createPoisson​(double mean)
      Creates the default Poisson distribution using the default random number generator.
      static cern.jet.random.PoissonSlow createPoissonSlow​(double mean)
      Creates the default slower Poisson distribution using the default random number generator.
      static cern.jet.random.StudentT createStudentT​(double freedom)
      Creates the default StudentT distribution using the default random number generator.
      static cern.jet.random.Uniform createUniform()
      Creates the default Unifrom distribution using the default random number generator.
      static cern.jet.random.Uniform createUniform​(double min, double max)
      Creates the default uniform distribution using the default random number generator.
      static cern.jet.random.VonMises createVonMises​(double freedom)
      Creates a default VonMises distribution using the default random number generator.
      static cern.jet.random.Zeta createZeta​(double ro, double pk)  
      static cern.jet.random.Beta getBeta()
      Gets the default beta distribution.
      static cern.jet.random.Binomial getBinomial()
      Gets the default binomial distribution.
      static cern.jet.random.BreitWigner getBreitWigner()
      Gets the default BreitWigner distribution.
      static cern.jet.random.BreitWignerMeanSquare getBreitWignerMeanSquare()
      Gets the default BreitWignerMeanSquare distribution.
      static cern.jet.random.ChiSquare getChiSquare()
      Gets the default Zeta distribution.
      static RandomRegistry getDefaultRegistry()
      Gets the default random registry.
      static cern.jet.random.AbstractDistribution getDistribution​(String name)
      Gets the named previously registered distribution.
      static cern.jet.random.Empirical getEmpirical()
      Gets the default ChiSquare distribution.
      static cern.jet.random.EmpiricalWalker getEmpiricalWalker()
      Gets the default EmpiricalWalker distribution.
      static cern.jet.random.Exponential getExponential()
      Gets the default Exponential distribution.
      static cern.jet.random.ExponentialPower getExponentialPower()
      Gets the default exponentialPower distribution.
      static cern.jet.random.Gamma getGamma()
      Gets the default gamma distribution.
      static cern.jet.random.engine.RandomEngine getGenerator()
      Gets the random number generator used by the default distributions.
      static cern.jet.random.engine.RandomEngine getGenerator​(String generatorName)
      Gets a previously registered random number generator.
      static cern.jet.random.Hyperbolic getHyperbolic()
      Gets the default hyperbolic distribution.
      static cern.jet.random.HyperGeometric getHyperGeometric()
      Gets the default hyperGeometric distribution.
      static cern.jet.random.Logarithmic getLogarithmic()
      Gets the default logarithmic distribution.
      static cern.jet.random.NegativeBinomial getNegativeBinomial()
      Gets the default negativeBinomial distribution.
      static cern.jet.random.Normal getNormal()
      Gets the default normal distribution.
      static cern.jet.random.Poisson getPoisson()
      Gets the default poisson distribution.
      static cern.jet.random.PoissonSlow getPoissonSlow()
      Gets the default slow poisson distribution.
      static int getSeed()
      Gets the seed used by the default random number generator.
      static int getSeed​(String generatorName)
      Gets the seed of the named generator.
      static cern.jet.random.StudentT getStudentT()
      Gets the default studentT distribution.
      static cern.jet.random.Uniform getUniform()
      Gets the default uniform distribution.
      static cern.jet.random.VonMises getVonMises()
      Gets the default vonMises distribution.
      static cern.jet.random.Zeta getZeta()
      Gets the default Zeta distribution.
      static void init()
      Initializes the random helper.
      static double nextDouble()
      This retrieves the next double from the default uniform stream.
      static double nextDoubleFromTo​(double from, double to)
      This retrieves the next double in the specified range from the default uniform stream.
      static int nextInt()
      This retrieves the next integer from the default uniform stream.
      static int nextIntFromTo​(int from, int to)
      This retrieves the next integer in the specified range from the default uniform stream.
      static void registerDistribution​(String name, cern.jet.random.AbstractDistribution dist)
      Registers the named random number distribution for later retrieval.
      static cern.jet.random.engine.RandomEngine registerGenerator​(String name, int seed)
      Creates and registers a new random number generator with the specified name and seed.
      static void setSeed​(int seed)
      This sets the seed for the default stream.
    • Constructor Detail

      • RandomHelper

        public RandomHelper()
    • Method Detail

      • createBeta

        public static cern.jet.random.Beta createBeta​(double alpha,
                                                      double beta)
        Creates the default beta distribution using the default random number generator.
        Parameters:
        alpha -
        beta -
        Returns:
        the created distribution
      • createBinomial

        public static cern.jet.random.Binomial createBinomial​(int n,
                                                              double p)
        Creates the default binomial distribution using the default random number generator.
        Parameters:
        n -
        p -
      • createBreitWigner

        public static cern.jet.random.BreitWigner createBreitWigner​(double mean,
                                                                    double gamma,
                                                                    double cut)
        Creates the default Breit Wigner distribution using the default random number generator.
        Parameters:
        mean -
        gamma -
        cut -
        Returns:
        the created distribution
      • createBreitWignerMeanSquare

        public static cern.jet.random.BreitWignerMeanSquare createBreitWignerMeanSquare​(double mean,
                                                                                        double gamma,
                                                                                        double cut)
        Creates the default Breit Wigner mean square state distribution using the default random number generator.
        Parameters:
        mean -
        gamma -
        cut -
        Returns:
        the created distribution
      • createChiSquare

        public static cern.jet.random.ChiSquare createChiSquare​(double freedom)
        Creates the default chi square distribution using the default random number generator.
        Parameters:
        freedom -
        Returns:
        the created distribution
      • createEmpirical

        public static cern.jet.random.Empirical createEmpirical​(double[] pdf,
                                                                int interpolationType)
        Creates the default empirical distribution using the default random number generator.
        Parameters:
        pdf -
        interpolationType -
        Returns:
        the created distribution
      • createEmpiricalWalker

        public static cern.jet.random.EmpiricalWalker createEmpiricalWalker​(double[] pdf,
                                                                            int interpolationType)
        Creates the default empirical walker distribution using the default random number generator.
        Parameters:
        pdf -
        interpolationType -
        Returns:
        the created distribution
      • createExponential

        public static cern.jet.random.Exponential createExponential​(double lambda)
        Creates the default exponential distribution using the default random number generator.
        Parameters:
        lambda -
        Returns:
        the created distribution
      • createExponentialPower

        public static cern.jet.random.ExponentialPower createExponentialPower​(double tau)
        Creates the default exponential power distribution using the default random number generator.
        Parameters:
        tau -
        Returns:
        the created distribution
      • createGamma

        public static cern.jet.random.Gamma createGamma​(double alpha,
                                                        double lambda)
        Creates the default gamma distribution using the default random number generator.
        Parameters:
        alpha -
        lambda -
        Returns:
        the created distribution
      • createHyperbolic

        public static cern.jet.random.Hyperbolic createHyperbolic​(double alpha,
                                                                  double beta)
        Creates the default hyperbolic distribution using the default random number generator.
        Parameters:
        alpha -
        beta -
        Returns:
        the created distribution
      • createHyperGeometric

        public static cern.jet.random.HyperGeometric createHyperGeometric​(int N,
                                                                          int s,
                                                                          int n)
        Creates the default hyper geometric distribution using the default random number generator.
        Parameters:
        N -
        s -
        n -
        Returns:
        the created distribution
      • createLogarithmic

        public static cern.jet.random.Logarithmic createLogarithmic​(double p)
        Creates the default logarithmic distribution using the default random number generator.
        Parameters:
        p -
        Returns:
        the created distribution
      • createNegativeBinomial

        public static cern.jet.random.NegativeBinomial createNegativeBinomial​(int n,
                                                                              double p)
        Creates the default negative binomial distribution using the default random number generator.
        Parameters:
        n -
        p -
        Returns:
        the created distribution
      • createNormal

        public static cern.jet.random.Normal createNormal​(double mean,
                                                          double standardDeviation)
        Creates the default normal distribution using the default random number generator.
        Parameters:
        mean -
        standardDeviation -
        Returns:
        the created distribution
      • createPoisson

        public static cern.jet.random.Poisson createPoisson​(double mean)
        Creates the default Poisson distribution using the default random number generator.
        Parameters:
        mean -
        Returns:
        the created distribution
      • createPoissonSlow

        public static cern.jet.random.PoissonSlow createPoissonSlow​(double mean)
        Creates the default slower Poisson distribution using the default random number generator.
        Parameters:
        mean -
        Returns:
        the created distribution
      • createStudentT

        public static cern.jet.random.StudentT createStudentT​(double freedom)
        Creates the default StudentT distribution using the default random number generator.
        Parameters:
        freedom -
        Returns:
        the created distribution
      • createUniform

        public static cern.jet.random.Uniform createUniform()
        Creates the default Unifrom distribution using the default random number generator.
        Returns:
        the created distribution
      • createUniform

        public static cern.jet.random.Uniform createUniform​(double min,
                                                            double max)
        Creates the default uniform distribution using the default random number generator.
        Parameters:
        min -
        max -
        Returns:
        the created distribution
      • createVonMises

        public static cern.jet.random.VonMises createVonMises​(double freedom)
        Creates a default VonMises distribution using the default random number generator.
        Parameters:
        freedom -
        Returns:
        the created distribution
      • createZeta

        public static cern.jet.random.Zeta createZeta​(double ro,
                                                      double pk)
      • getBeta

        public static cern.jet.random.Beta getBeta()
        Gets the default beta distribution.
        Returns:
        the default beta distribution.
      • getBinomial

        public static cern.jet.random.Binomial getBinomial()
        Gets the default binomial distribution.
        Returns:
        the default binomial distribution.
      • getBreitWigner

        public static cern.jet.random.BreitWigner getBreitWigner()
        Gets the default BreitWigner distribution.
        Returns:
        the default BreitWigner distribution.
      • getBreitWignerMeanSquare

        public static cern.jet.random.BreitWignerMeanSquare getBreitWignerMeanSquare()
        Gets the default BreitWignerMeanSquare distribution.
        Returns:
        the default BreitWignerMeanSquare distribution.
      • getChiSquare

        public static cern.jet.random.ChiSquare getChiSquare()
        Gets the default Zeta distribution.
        Returns:
        the default Zeta distribution.
      • getEmpirical

        public static cern.jet.random.Empirical getEmpirical()
        Gets the default ChiSquare distribution.
        Returns:
        the default ChiSquare distribution.
      • getEmpiricalWalker

        public static cern.jet.random.EmpiricalWalker getEmpiricalWalker()
        Gets the default EmpiricalWalker distribution.
        Returns:
        the default EmpiricalWalker distribution.
      • getExponential

        public static cern.jet.random.Exponential getExponential()
        Gets the default Exponential distribution.
        Returns:
        the default Exponential distribution.
      • getExponentialPower

        public static cern.jet.random.ExponentialPower getExponentialPower()
        Gets the default exponentialPower distribution.
        Returns:
        the default exponentialPower distribution.
      • getGamma

        public static cern.jet.random.Gamma getGamma()
        Gets the default gamma distribution.
        Returns:
        the default gamma distribution.
      • getHyperbolic

        public static cern.jet.random.Hyperbolic getHyperbolic()
        Gets the default hyperbolic distribution.
        Returns:
        the default hyperbolic distribution.
      • getHyperGeometric

        public static cern.jet.random.HyperGeometric getHyperGeometric()
        Gets the default hyperGeometric distribution.
        Returns:
        the default hyperGeometric distribution.
      • getLogarithmic

        public static cern.jet.random.Logarithmic getLogarithmic()
        Gets the default logarithmic distribution.
        Returns:
        the default logarithmic distribution.
      • getNegativeBinomial

        public static cern.jet.random.NegativeBinomial getNegativeBinomial()
        Gets the default negativeBinomial distribution.
        Returns:
        the default negativeBinomial distribution.
      • getNormal

        public static cern.jet.random.Normal getNormal()
        Gets the default normal distribution.
        Returns:
        the default normal distribution.
      • getPoisson

        public static cern.jet.random.Poisson getPoisson()
        Gets the default poisson distribution.
        Returns:
        the default poisson distribution.
      • getPoissonSlow

        public static cern.jet.random.PoissonSlow getPoissonSlow()
        Gets the default slow poisson distribution.
        Returns:
        the default slow poisson distribution.
      • getStudentT

        public static cern.jet.random.StudentT getStudentT()
        Gets the default studentT distribution.
        Returns:
        the default studentT distribution.
      • getUniform

        public static cern.jet.random.Uniform getUniform()
        Gets the default uniform distribution.
        Returns:
        the default uniform distribution.
      • getVonMises

        public static cern.jet.random.VonMises getVonMises()
        Gets the default vonMises distribution.
        Returns:
        the default vonMises distribution.
      • getZeta

        public static cern.jet.random.Zeta getZeta()
        Gets the default Zeta distribution.
        Returns:
        the default Zeta distribution.
      • setSeed

        public static void setSeed​(int seed)
        This sets the seed for the default stream. This is the same as DEFAULT_REGISTRY.setSeed(RandomHelper.DEFAULT_STREAM, seed);

        The default stream will be created if it has not yet been.

        Parameters:
        seed - the seed
      • nextDoubleFromTo

        public static double nextDoubleFromTo​(double from,
                                              double to)
        This retrieves the next double in the specified range from the default uniform stream.
        Parameters:
        from - the start of the range (exclusive)
        to - the end of the range (exclusive)
        Returns:
        the next double from the default uniform stream
      • nextDouble

        public static double nextDouble()
        This retrieves the next double from the default uniform stream. T
        Returns:
        the next double from the default uniform stream
      • nextIntFromTo

        public static int nextIntFromTo​(int from,
                                        int to)
        This retrieves the next integer in the specified range from the default uniform stream.
        Parameters:
        from - the start of the range (inclusive)
        to - the end of the range (inclusive)
        Returns:
        the next int from the default uniform stream
      • nextInt

        public static int nextInt()
        This retrieves the next integer from the default uniform stream.
        Returns:
        the next int from the default uniform stream
      • init

        public static void init()
        Initializes the random helper. This will invalidate any previously created distributions, both default and custom) and set the current seed for default generator to the current system time. This will also recreate the default uniform distribution.
      • getDefaultRegistry

        public static RandomRegistry getDefaultRegistry()
        Gets the default random registry.
        Returns:
        the default random registry
      • getGenerator

        public static cern.jet.random.engine.RandomEngine getGenerator()
        Gets the random number generator used by the default distributions.
        Returns:
        the random number generator used by the default distributions.
      • getSeed

        public static int getSeed()
        Gets the seed used by the default random number generator.
        Returns:
        the seed used by the default random number generator.
      • registerGenerator

        public static cern.jet.random.engine.RandomEngine registerGenerator​(String name,
                                                                            int seed)
        Creates and registers a new random number generator with the specified name and seed. The generator can then be retrieved later using #getGenerator(String).

        Parameters:
        name - the name of the generator to create and register
        seed - the new generators seed
        Returns:
        the new generator itself
      • getGenerator

        public static cern.jet.random.engine.RandomEngine getGenerator​(String generatorName)
        Gets a previously registered random number generator.
        Parameters:
        generatorName - the name of the generator to get
        Returns:
        a previously registered random number generator.
      • getSeed

        public static int getSeed​(String generatorName)
        Gets the seed of the named generator.
        Parameters:
        generatorName - the name of the generator
        Returns:
        the seed of the named generator.
      • registerDistribution

        public static void registerDistribution​(String name,
                                                cern.jet.random.AbstractDistribution dist)
        Registers the named random number distribution for later retrieval.
        Parameters:
        name - the name of the distribution
        dist - the distribution to register
      • getDistribution

        public static cern.jet.random.AbstractDistribution getDistribution​(String name)
        Gets the named previously registered distribution. Calling code can then cast the distribution into the appropriate type (e.g. a Normal).
        Parameters:
        name - the name of the distribution to get
        Returns:
        the named previously registered distribution.