Generating a random double in an exclusive range

倖福魔咒の 提交于 2019-12-13 07:10:08

问题


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


回答1:


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 <=



回答2:


Try ThreadLocalRandom.current().nextDouble(origin + 1, bound). Per the javadoc, min is inclusive and max is exclusive by default



来源:https://stackoverflow.com/questions/39575237/generating-a-random-double-in-an-exclusive-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!