我如何在指定范围内获得随机双倍? [英] how do i get a random double within a specified range?
问题描述
我真的以为这会行得通,但是当我运行它时,我总是得到一些随机的大十进制数,这毫无意义。逻辑是正确的,所以代码为什么不起作用?
I really thought this would work, but when I ran it, I always get some random huge decimal and it makes no sense. The logic is right, so why doesn't the code work?
do{
value = rnd.nextDouble();
}while(value>min && value<max);
请记住, min和 max实际上是执行一些简单算术运算的函数,然后返回该值,但为简单起见,我只输入了最小值和最大值,因此不必发布整个类。
Keep in mind "min" and "max" are actually functions that do some simple arithmetic operations then return that value, but I just put min and max for simplicity so I don't have to post the entire class.
推荐答案
您可以声明 start
和 end
变量( int
或 double
),并使用 nextDouble()
像:
You can declare a start
and end
variables (int
or double
), and use nextDouble()
like:
value = rnd.nextDouble() * (end - start) + start;
请记住:
公共double nextDouble()
返回下一个伪随机数,此随机数从此随机数生成器的序列中均匀分布在0.0和1.0之间。
public double nextDouble() Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.
这篇关于我如何在指定范围内获得随机双倍?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!