Package de.gsi.math
Class TRandom
- java.lang.Object
-
- de.gsi.math.TRandom
-
public class TRandom extends java.lang.ObjectTRandom 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: ===================================================
-
-
Field Summary
Fields Modifier and Type Field Description protected static longfSeed
-
Constructor Summary
Constructors Constructor Description TRandom(long seed)default constructor
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intBinomial(int ntot, double prob)doubleBreitWigner(double mean, double gamma)Return a number distributed following a BreitWigner function with mean and gammavoidCircle(double[] val, double r)generates random vectors, uniformly distributed over a circle of given radius.doubleExp(double tau)doubleGaus(double mean, double sigma)samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigma.longInteger(long imax)returns a random integer on [ 0, imax-1 ].doubleLandau(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)intPoisson(double mean)Generates a random integer N according to a Poisson law.doublePoissonD(double mean)Generates a random number according to a Poisson law.voidRannor(double[] val)Return 2 numbers distributed following a gaussian with mean=0 and sigma=1voidRannor(float[] val)Return 2 numbers distributed following a gaussian with mean=0 and sigma=1doubleRndm()Machine independent random number generator.voidRndmArray(int n, float[] array)Return an array of n random numbers uniformly distributed in ]0,1]voidSetSeed(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.voidSphere(double[] val, double r)generates random vectors, uniformly distributed over the surface of a sphere of given radius.java.lang.StringtoString()doubleUniform(double x1)returns a uniform deviate on the interval ]0, x1].doubleUniform(double x1, double x2)returns a uniform deviate on the interval ]x1, x2].
-
-
-
Method Detail
-
Binomial
public int Binomial(int ntot, double prob)
-
BreitWigner
public double BreitWigner(double mean, double gamma)Return a number distributed following a BreitWigner function with mean and gamma- Parameters:
mean- center of Breit-Wigner distributiongamma- width of Breit-Wigner distribution- Returns:
- number distributed following a BreitWigner function
-
Circle
public 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 rr- circle radius
-
Exp
public double Exp(double tau)
- Parameters:
tau- parameter- Returns:
- exponential deviate. exp( -t/tau )
-
Gaus
public 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 functionsigma- width of Gauss function- Returns:
- random number from the standard Normal (Gaussian) Distribution
-
Integer
public 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 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 distributionsigma- parameter of Landau distribution- Returns:
- random number following a Landau distribution
-
Poisson
public 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 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 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 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 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 arrayarray- 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 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 rr- sphere radius
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
Uniform
public 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 double Uniform(double x1, double x2)returns a uniform deviate on the interval ]x1, x2].- Parameters:
x1- minimum rangex2- maximum range- Returns:
- uniform deviate on the interval ]x1, x2]
-
-