Fredrik Bakke - 1 year ago 88
Java Question

# Generating a random double in an exclusive range

I'm trying to generate a random double between but not including it's lower and upper bound (lower, upper).
I've seen a lot of questions about generating a number from, and including it's lower bound up to, but not including it's upper bound [lower, upper), but they don't answer my question as they do not address this case.

I've come up with two "solutions" to the problem, but am not really satisfied by either.

## First "solution"

``````double myvalue;
do { myvalue = myrandom.nextDouble() * (upper - lower) + lower; } while (myvalue == lower);
``````

While this will almost everytime give a valid result on the first try it seems inconsistent and clunky, and on the off-chance that the rng returns 0.0 a lot, it's inefficient aswell.

## Second "solution"

``````double myvalue =
0.5 * (1.0 + myrandom.nextDouble() - myrandom.nextDouble()) * (upper - lower) + lower;
``````

While this will guarantee me a valid number on the first try, I don't believe the distribution is uniform in this case.

Thanks

Your first code is quite correct.

``````double myvalue;
do {
myvalue = myrandom.nextDouble() * (upper - lower) + lower;
} while (myvalue == lower);  // Replace != with ==
``````

Simply replace `!=` with `==`. It is not true that this is not efficient.

A look to efficiency of the code.

Taking a look on the code of `nextDouble` you can see that it is calculated generating a random long value.

It means that basically you will have 1 possibility to repeat the loop over 2 * 9,223,372,036,854,775,807 (the range of long values minus 1).

Additional Note: because you are using `double` and not a `BigDecimal` you can obtain a `myvalue` that is close to the `lower` bound, but lower than it, so it is better to change the code to exclude also values lower than `lower`. It doesn't change the efficiency of your code.

``````double myvalue;
do {
myvalue = myrandom.nextDouble() * (upper - lower) + lower;
} while (myvalue <= lower);  // Replace != with <=
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download