Pseudorandom Number Generator - Exponential Distribution
Since you have access to a uniform random number generator, generating a random number distributed with other distribution whose CDF you know is easy using the inversion method.
So, generate a uniform random number, u
, in [0,1)
, then calculate x
by:
x = log(1-u)/(-λ)
,
where λ
is the rate parameter of the exponential distribution. Now, x
is a random number with an exponential distribution. Note that log
above is ln
, the natural logarithm.
The Fundamental Theorem of Sampling holds that if you can normalize, integrate and invert the desired distribution you are home free.
If you have a desired distribution F(x)
normalized on [a,b]
. You compute
C(y) = \int_a^y F(x) dx
invert that to get C^{-1}
, throw z
uniformly on [0,1) and find
x_i = C^{-1}(z_i)
which will have the desired distribution.
In your case: F(x) = ke^{-kx}
and I will assume that you want [0,infinity]
. We get :
C(y) = 1 - e^{-ky}
which is invertable to give
x = -1/k ln(1 - z)
for z thrown uniformly on [0,1)
.
But, frankly, using a well debugged library is smarter unless you're doing this for your own edification.
This is the formula I found on Wikipedia :
T = -Ln(u) / λ
We create a random number with a uniform distribution (u) in [0,1]and we get x :
Random R = new Random();
double u = R. NextDouble();
double x = -Math.Log(u)/(λ);