Skip to content
Advertisement

Generating a random double in an exclusive range

I’m trying to generate a random floating point number between but not including it’s lower and upper bounds (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 that’s not what I’m after.

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 zero a lot, it’s inefficient as well.

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

Advertisement

Answer

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 <=
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement