Database Reference
In-Depth Information
u to yield a simple implementation for drawing from the exponential
distribution:
public double nextExponential( double p) {
return -Math. log (rng.nextDouble())/p;
}
A very similar approach can be taken when generating Poisson random
variables. In the case of the Poisson distribution, the CDF contains a
summation step so it is necessary to iterate through values of k until the
appropriate value is found, as in the following implementation:
public int nextPoisson( double p) {
int k = 0;
double c = 0, u = rng.nextDouble()/Math. exp (-p);
while ( true ) {
double x = c + Math. pow (p, k)
/Arithmetic. factorial (k);
if (x > u) return k;
c += x;
k++;
}
}
Normal Random Numbers
If the cumulative density function does not have an easy-to-use closed form,
it is sometimes possible to take advantage of other transforms of a uniform
drawtoproducerandomdrawsfromadistribution.Aclassicexampleofthis
is the Box-Muller method for drawing from a normal distribution.
This transform works by drawing a uniformly distributed angle on a unit
circle by drawing a uniform variate in [0,2pi] and drawing a radius from the
chi-square distribution with 2 degrees of freedom. This is actually a special
case of the exponential distribution where p = ½. This position in polar
space is then converted to Cartesian coordinates, yielding two independent
normal random variables (the second can be used on the next draw):
double z2;
boolean ready = false;
Search WWH ::




Custom Search