在Java中生成一定范围的随机双数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9723765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generating a random double number of a certain range in Java
提问by isaiah
I have seen posts which explains pretty much this question but they all used integer values and I honestly do not fully understand it hence this question:
我看过几乎解释了这个问题的帖子,但他们都使用了整数值,老实说,我并不完全理解它,因此这个问题:
I am trying to generate random numbers from the range (-1554900.101) to (52952058699.3098) in java and I was wondering if anyone could explain this to me as I really want to understand it.
我试图在 java 中生成从 (-1554900.101) 到 (52952058699.3098) 范围内的随机数,我想知道是否有人可以向我解释这一点,因为我真的很想理解它。
My thoughts: will this be a right approach? 1) Generate a random integer number within my specified range 2) Divide the generated number by pi to get float/double random results
我的想法:这是一个正确的方法吗?1) 在我指定的范围内生成一个随机整数 2) 将生成的数字除以 pi 得到浮点/双随机结果
Thanks in advance.
提前致谢。
采纳答案by maerics
Here's the idea. You want a random number in a range, let's say [-1.1,2.2]
, to start with a simple example. That range has length 3.3 since 2.2 - (-1.1) = 3.3
. Now most "random" functions return a number in the range [0,1)
, which has length one, so we have to scaleour random number into our desired range.
这是想法。你想要一个范围内的随机数,比如说[-1.1,2.2]
,从一个简单的例子开始。该范围的长度为 3.3,因为2.2 - (-1.1) = 3.3
. 现在大多数“随机”函数返回一个范围内的数字[0,1)
,其长度为 1,因此我们必须将随机数缩放到我们想要的范围内。
Random random = new Random();
double rand = random.nextDouble();
double scaled = rand * 3.3;
Now our random number has the magnitude we want but we must shift it in the number line to be between the exact values we want. For this step, we just need to add the lower bound of the entire range to our scaled random number and we're done!
现在我们的随机数具有我们想要的大小,但我们必须在数轴中将它移动到我们想要的确切值之间。对于这一步,我们只需要将整个范围的下限添加到我们的缩放随机数中,我们就完成了!
double shifted = scaled + (-1.1);
So now we can put these parts together in a single function:
所以现在我们可以将这些部分放在一个函数中:
protected static Random random = new Random();
public static double randomInRange(double min, double max) {
double range = max - min;
double scaled = random.nextDouble() * range;
double shifted = scaled + min;
return shifted; // == (rand.nextDouble() * (max-min)) + min;
}
Of course, this function needs some error checking for unexpected values like NaN
but this answer should illustrate the general idea.
当然,此函数需要对意外值进行一些错误检查,例如,NaN
但此答案应说明总体思路。
回答by highlycaffeinated
double lower = -1554900.101;
double upper = 52952058699.3098;
double result = Math.random() * (upper - lower) + lower;
回答by xanatos
It should be something like:
它应该是这样的:
double rnd = Math.random();
double result = ((long)(rnd * (529520586993098L - (-15549001010L) + 1)) -15549001010L) / 10000.0;
The + 1
will balance for the fact that the range is [0;1[
so the upper range is excluded.
在+ 1
将平衡的事实,所述范围是[0;1[
如此的上限范围被排除。
We first try to find an "integer" (really long) range and we add 1 to balance the fact that the last number is excluded, so 529520586993098L + 15549001010L + 1
, then we multiply it by rnd
, cast it to long
and subtract 15549001010L
to shift it back and in the end divide it by 10000.0
to make it in the right "range".
我们首先尝试找到一个“整数”(非常长的)范围,然后加 1 以平衡排除最后一个数字的事实,因此529520586993098L + 15549001010L + 1
,我们将其乘以rnd
,将其转换为long
并减去15549001010L
以将其移回并最终除以使其10000.0
在正确的“范围”内。
This is probably more clear:
这可能更清楚:
long range = 529520586993098L + 15549001010L + 1;
double temp = rnd * range;
long temp2 = (long)temp;
temp2 -= 15549001010L;
double result = temp2 / 10000.0;
回答by JB Nizet
This not how I would do it.
这不是我会怎么做。
- Generate a random double. The result is between 0 and 1.
- Multiply this number by (highLimit - lowLimit) (52952058699.3098 - -1554900.101)
- Add the lowLimit (random + -1554900.101)
- 生成一个随机双。结果介于 0 和 1 之间。
- 将此数字乘以 (highLimit - lowLimit) (52952058699.3098 - -1554900.101)
- 添加低限(随机 + -1554900.101)
Here you go. You have a random number between low and high limit.
干得好。您有一个介于上下限之间的随机数。
回答by Roger Lindsj?
Random.nextDoublereturns a double in the range [0, 1[ so use that, multiply with your range size (52952058699.3098 + 1554900.101) and then offset the result (subtract 1554900.101) to get your number.
Random.nextDouble返回 [0, 1[ 范围内的双精度数,因此使用它,乘以您的范围大小 (52952058699.3098 + 1554900.101),然后抵消结果(减去 1554900.101)以获得您的数字。
Not sure how exact you need this though, without doing some further analysis you might get numbers outside your range due to how doubles are handled.
不确定你需要这个有多精确,如果不做一些进一步的分析,由于双打的处理方式,你可能会得到超出范围的数字。