Exponential distribution in Java not right - values too small?

566 Views Asked by At

I am trying to generate an exponential distribution for arrival and service times of processes. In C++, the example I have works fine and generates pseudo-random numbers in the range [0, inf) and some are bigger as expected. In Java, it does not work. The numbers are orders of magnitude smaller than their C++ equivalents, and I NEVER get any values > 0.99 even though I am using the same formula. In C++ I get 1.xx, or 2.xx etc., but never in Java.

lambda is the average rate of arrival and gets varied from 1 to 30. I know that rand.nextDouble() gives a value b/w 0 and 1 and from the formula given and answers here on this site, this seems to be a needed component.

I should mention that multiplying my distribution values by 10 gets me much closer to where they need to be and they behave as expected.

In Java:

Random rand = new Random();
// if I multiply x by 10, I get much closer to the distribution I need
// I just don't know why it's off by a factor of 10?!
x =  (Math.log(1-rand.nextDouble())/(-lambda));

I have also tried:

x = 0;
while (x == 0)
{
   x = (-1/lambda)*log(rand.nextDouble());
}

The C++ code I was given:

// returns a random number between 0 and 1
float urand()
{
    return( (float) rand()/RAND_MAX );
}

// returns a random number that follows an exp distribution
float genexp(float lambda)
{
    float u,x;
    x = 0;
    while (x == 0)
        {
            u = urand();
            x = (-1/lambda)*log(u);
        }
    return(x);
}
0

There are 0 best solutions below