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