0

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);
}
Boris
  • 566
  • 5
  • 18
  • `rand.nextDouble()` returns number between 0 and 1 in Java. – Mershel Nov 05 '18 at 15:38
  • Yes it does, the formula I obtained from a Stackoverflow post and from some papers, so it seems to be an accepted one. The pseudo-random value between 0 and 1 is used to generate the exponential distribution. Lambda is the average rate of arrival and I vary it between 1 and 30. – Boris Nov 05 '18 at 15:47
  • Are you sure the formula you have implemented is correct? It seems to be missing a factor of `1/lambda` inside the logarithm ([source](https://en.wikipedia.org/wiki/Exponential_distribution#Probability_density_function)); unless you're trying to implement the *cumulative* distribution. – meowgoesthedog Nov 05 '18 at 15:47
  • I am not sure how to read that page, I was told to pass in just lambda to the genexp function to get an arrival time and 1/avg service time as the "lambda" to get a service time. Could you please show me what my formula should look like? Where does 1/lambda go? Thank you! – Boris Nov 05 '18 at 15:53
  • How many values do you compare? With `lambda = 2` and 1000 values your Java code does give me also values larger than one. And the mean is (as expected) 1/2. – Ralf Stubner Nov 05 '18 at 20:17
  • @RalfStubner I did 10,000 values and all of them end up 0.0.. or 0.00... or even smaller. That is interesting. What I ended up doing is multiply by 10 to shift them up. – Boris Nov 06 '18 at 00:45
  • Very odd, but impossible to debug without a [mcve]. – Ralf Stubner Nov 06 '18 at 06:42

0 Answers0