Package de.gsi.math

Class TRandom

  • Direct Known Subclasses:
    TRandom1, TRandom3

    public class TRandom
    extends java.lang.Object
    TRandom basic Random number generator class (periodicity = 10**9). Note that this is a very simple generator (linear congruential) which is known to have defects (the lower random bits are correlated) and therefore should NOT be used in any statistical study. One should use instead TRandom1, TRandom2 or TRandom3. TRandom3, is based on the "Mersenne Twister generator", and is the recommended one, since it has good random proprieties (period of about 10**6000 ) and it is fast. TRandom1, based on the RANLUX algorithm, has mathematically proven random proprieties and a period of about 10**171. It is however slower than the others. TRandom2, is based on the Tausworthe generator of L'Ecuyer, and it has the advantage of being fast and using only 3 words (of 32 bits) for the state. The period is 10**26. The following basic Random distributions are provided: ===================================================
    See Also:
    Exp(double), Integer(long), Gaus(double, double), Rndm(), Uniform(double), Landau(double, double), Poisson(double), Binomial(int, double)
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static long fSeed  
    • Constructor Summary

      Constructors 
      Constructor Description
      TRandom​(long seed)
      default constructor
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static int Binomial​(int ntot, double prob)  
      static double BreitWigner​(double mean, double gamma)
      Return a number distributed following a BreitWigner function with mean and gamma
      static void Circle​(double[] val, double r)
      generates random vectors, uniformly distributed over a circle of given radius.
      static double Exp​(double tau)  
      static double Gaus​(double mean, double sigma)
      samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigma.
      static long Integer​(long imax)
      returns a random integer on [ 0, imax-1 ].
      static double Landau​(double mpv, double sigma)
      Generate a random number following a Landau distribution with mpv(most probable value) and sigma Converted by Rene Brun from CERNLIB routine ranlan(G110)
      static int Poisson​(double mean)
      Generates a random integer N according to a Poisson law.
      static double PoissonD​(double mean)
      Generates a random number according to a Poisson law.
      static void Rannor​(double[] val)
      Return 2 numbers distributed following a gaussian with mean=0 and sigma=1
      static void Rannor​(float[] val)
      Return 2 numbers distributed following a gaussian with mean=0 and sigma=1
      static double Rndm()
      Machine independent random number generator.
      void RndmArray​(int n, float[] array)
      Return an array of n random numbers uniformly distributed in ]0,1]
      void SetSeed​(long seed)
      Set the random generator seed if seed is zero, the seed is set to the current machine clock Note that the machine clock is returned with a precision of 1 second.
      static void Sphere​(double[] val, double r)
      generates random vectors, uniformly distributed over the surface of a sphere of given radius.
      java.lang.String toString()  
      static double Uniform​(double x1)
      returns a uniform deviate on the interval ]0, x1].
      static double Uniform​(double x1, double x2)
      returns a uniform deviate on the interval ]x1, x2].
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • fSeed

        protected static long fSeed
    • Constructor Detail

      • TRandom

        public TRandom​(long seed)
        default constructor
        Parameters:
        seed - initial seed
    • Method Detail

      • Binomial

        public static int Binomial​(int ntot,
                                   double prob)
      • BreitWigner

        public static double BreitWigner​(double mean,
                                         double gamma)
        Return a number distributed following a BreitWigner function with mean and gamma
        Parameters:
        mean - center of Breit-Wigner distribution
        gamma - width of Breit-Wigner distribution
        Returns:
        number distributed following a BreitWigner function
      • Circle

        public static void Circle​(double[] val,
                                  double r)
        generates random vectors, uniformly distributed over a circle of given radius.
        Parameters:
        val - [0], [1] a random 2-d vector of length r
        r - circle radius
      • Exp

        public static double Exp​(double tau)
        Parameters:
        tau - parameter
        Returns:
        exponential deviate. exp( -t/tau )
      • Gaus

        public static double Gaus​(double mean,
                                  double sigma)
        samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigma. Uses the Acceptance-complement ratio from W. Hoermann and G. Derflinger This is one of the fastest existing method for generating normal random variables. It is a factor 2/3 faster than the polar (Box-Muller) method used in the previous version of Gaus. The speed is comparable to the Ziggurat method (from Marsaglia) implemented for example in GSL and available in the MathMore library. REFERENCE: W. Hoermann and G. Derflinger (1990): The ACR Method for generating normal random variables, OR Spektrum 12 (1990), 181-185. Implementation taken from UNURAN (c) 2000 W. Hoermann & J. Leydold, Institut f. Statistik, WU Wien
        Parameters:
        mean - centre of Gauss function
        sigma - width of Gauss function
        Returns:
        random number from the standard Normal (Gaussian) Distribution
      • Integer

        public static long Integer​(long imax)
        returns a random integer on [ 0, imax-1 ].
        Parameters:
        imax - max range
        Returns:
        random integer on [ 0, imax-1 ]
      • Landau

        public static double Landau​(double mpv,
                                    double sigma)
        Generate a random number following a Landau distribution with mpv(most probable value) and sigma Converted by Rene Brun from CERNLIB routine ranlan(G110)
        Parameters:
        mpv - parameter of Landau distribution
        sigma - parameter of Landau distribution
        Returns:
        random number following a Landau distribution
      • Poisson

        public static int Poisson​(double mean)
        Generates a random integer N according to a Poisson law. Prob(N) = exp(-mean)*mean^N/Factorial(N) Use a different procedure according to the mean value. The algorithm is the same used by CLHEP For lower value (mean < 25) use the rejection method based on the exponential For higher values use a rejection method comparing with a Lorentzian distribution, as suggested by several authors This routine since is returning 32 bits integer will not work for values larger than 2*10**9 One should then use the TRandom.PoissonD for such large values
        Parameters:
        mean - centre of distribution
        Returns:
        random integer N according to a Poisson law
      • PoissonD

        public static double PoissonD​(double mean)
        Generates a random number according to a Poisson law. Prob(N) = exp(-mean)*mean^N/Factorial(N) This function is a variant of Poisson returning a double instead of an integer.
        Parameters:
        mean - centre of distribution
        Returns:
        random number according to a Poisson law
      • Rannor

        public static void Rannor​(double[] val)
        Return 2 numbers distributed following a gaussian with mean=0 and sigma=1
        Parameters:
        val - storage vector for Gaussian distributed random numbers
      • Rannor

        public static void Rannor​(float[] val)
        Return 2 numbers distributed following a gaussian with mean=0 and sigma=1
        Parameters:
        val - storage vector for Rannor distributed random numbers
      • Rndm

        public static double Rndm()
        Machine independent random number generator. Based on the BSD Unix (Rand) Linear congrential generator Produces uniformly-distributed floating points between 0 and 1. Identical sequence on all machines of >= 32 bits. Periodicity = 2**31 generates a number in ]0,1] Note that this is a generator which is known to have defects (the lower random bits are correlated) and therefore should NOT be used in any statistical study.
        Returns:
        machine independent random number
      • RndmArray

        public void RndmArray​(int n,
                              float[] array)
        Return an array of n random numbers uniformly distributed in ]0,1]
        Parameters:
        n - length of array
        array - storage array
      • SetSeed

        public void SetSeed​(long seed)
        Set the random generator seed if seed is zero, the seed is set to the current machine clock Note that the machine clock is returned with a precision of 1 second. If one calls SetSeed(0) within a loop and the loop time is less than 1s, all generated numbers will be identical!
        Parameters:
        seed - initial seed
      • Sphere

        public static void Sphere​(double[] val,
                                  double r)
        generates random vectors, uniformly distributed over the surface of a sphere of given radius. Method: (based on algorithm suggested by Knuth and attributed to Robert E Knop) which uses less random numbers than the CERNLIB RN23DIM algorithm
        Parameters:
        val - [0], [1], [2] a random 3-d vector of length r
        r - sphere radius
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • Uniform

        public static double Uniform​(double x1)
        returns a uniform deviate on the interval ]0, x1].
        Parameters:
        x1 - maximum range
        Returns:
        uniform deviate on the interval ]0, x1]
      • Uniform

        public static double Uniform​(double x1,
                                     double x2)
        returns a uniform deviate on the interval ]x1, x2].
        Parameters:
        x1 - minimum range
        x2 - maximum range
        Returns:
        uniform deviate on the interval ]x1, x2]