简体   繁体   English

给定范围内的随机双精度,包括[-MAX_VALUE,MAX_VALUE]

[英]Random Double in given range including [-MAX_VALUE,MAX_VALUE]

How do I implement a function getRandomDouble(min,max) which is able to handle +-Double.MAX_VALUE as parameter? 如何实现函数getRandomDouble(min,max) ,该函数能够处理+-Double.MAX_VALUE作为参数?

Online Research: 在线研究:

Most answers to this question are: 这个问题的大多数答案是:

public Double getRandomDouble(double min, double max) {
    return min + (max-min)*Random.nextDouble(); 
}

This works fine if min and max are not +-Double.MAX_VALUE . 如果minmax不是+-Double.MAX_VALUE则可以正常+-Double.MAX_VALUE If so, max-min is out of range or infinity. 如果是这样,则max-min超出范围或无穷大。 Changing the parameters to BigDecimal solves this issue, but the result is always a double with 50 zeros and no decimals. 将参数更改为BigDecimal可以解决此问题,但结果始终是带有50个零且没有小数的double。 This is the result of a very large number ( 2*MAX_VALUE ) multiplied a double between [0,1] with only a view decimals. 这是非常大的数字( 2*MAX_VALUE )将[0,1]之间的双倍数乘以仅查看小数的结果。

So, I found a solution for +-MAX_VALUE like this: 因此,我找到了+-MAX_VALUE的解决方案,如下所示:

public double getRandomDouble() {
    while(true) {
        double d = Double.longBitsToDouble(Random.nextLong());
        if (d < Double.POSITIVE_INFINITY && d > Double.NEGATIVE_INFINITY)
            return d;
    }
}

This works fine, but does not consider other bounds. 这可以正常工作,但不考虑其他范围。

How can I combine both approaches to get random double in a given range that's maybe +-MAX_VALUE ? 如何结合两种方法在给定范围内(可能为+-MAX_VALUE获得随机加倍?

If it's not working only with the max values, I think I have a solution. 如果不是仅使用最大值,我想我有一个解决方案。

Mathematically it should be the same: 数学上应该是相同的:

You produce 2 random numbers and sum them up. 您产生2个随机数,并将它们加起来。 Each number should be between -maxValue / 2 and +maxValue / 2 . 每个数字应在-maxValue / 2+maxValue / 2 That way the sum will be a random number between -maxValue and +maxValue . 这样,总和将是-maxValue+maxValue之间的随机数。

public Double getRandomDouble(double min, double max) {
    double halfMin = min/2;
    double halfMax = max/2;
    double sum = halfMin + (halfMax-halfMin)*Random.nextDouble(); 
    return sum + (halfMin + (halfMax-halfMin)*Random.nextDouble());
}

You can do this by drawing two double random numbers. 您可以通过绘制两个double随机数来实现。 The first one is used to decide if you want a negative value or a positive value. 第一个用于确定您要负值还是正值。 The second one is used for the actual value from the negative part or the positive part. 第二个用于负数部分或正数部分的实际值。 The approach will be like this: 方法将是这样的:

Calculate the quotient between the range from zero to upper bound and the range from zero to lower bound. 计算从零到上限的范围和从零到下限的范围之间的商。 This will get you the information like "40% the value is positive, 60% the value is negative". 这将为您提供诸如“ 40%的值为正,60%的值为负”之类的信息。 Use the first random number to check which it is. 使用第一个随机数检查它是哪个。

Then when you know if it is negative or positive use the normal approach to get a random number between zero and the lower/upper bound. 然后,当您知道它是负数还是正数时,请使用常规方法获取介于零和下限/上限之间的随机数。 This value can only be between 0 and MAX_VALUE (or MIN_VALUE ), so there will be no "overflow". 此值只能在0MAX_VALUE (或MIN_VALUE )之间,因此不会出现“溢出”。

However, I don't know about the combined probability in this case, if it is the same random probability when drawing only one random double value. 但是,在这种情况下,如果仅绘制一个随机double值时是否具有相同的随机概率,我就不会知道组合概率。 If you have a negative/positive split of about 5% and 95%, then the second random number will be hit a value inside the 5% or the 95%. 如果您的负/正拆分约为5%和95%,则第二个随机数将达到5%或95%内的值。 Not sure if it still "random" or if it even creates unwanted bias. 不知道它是否仍然是“随机的”,还是会产生不必要的偏差。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM