简体   繁体   English

如何在Java中生成指定范围之外的随机双数?

[英]How to generate random double number outside a specified range in Java?

Is there a way to generate random double value outside a specified range?有没有办法在指定范围之外生成随机双精度值?

I know there is one within the range:我知道范围内有一个:

Random r = new Random();
double randomValue = rangeMin + (rangeMax - rangeMin) * r.nextDouble();

I would require one that is outside the range eg我需要一个超出范围的,例如

the range is 20 - 50 and I would like a number below 20 or higher than 50.范围是 20 - 50,我想要一个低于 20 或高于 50 的数字。

Could someone please advise?有人可以建议吗?

Maybe something like (for numbers 1-20 and 50-100):也许类似(对于数字 1-20 和 50-100):

Random r = new Random();
double randomValue = r.nextDouble()*70;
if(randomValue>20) randomValue+=30;

It is not resource expensive and easy to understand.它不是资源昂贵且易于理解。

You can try somethink like this :你可以试试这样的想法:

Random rnd = new Random();
double x=0;
do{
    x = rnd.nextDouble()*100;
}while(x>20 && x<50 );

    System.out.println(x);
    }

You generate a random double ( need multiply by 100 because generate double return value between 0 and 1 ) and loop while result >20 and <50您生成一个随机双精度(需要乘以 100,因为在 0 和 1 之间生成双返回值)并在结果 >20 和 <50 时循环

If you want a double , any double , outside a specific range, then you you can take advantage of the fact that a double is represented by 64 bits, and you can convert any 64-bit value to a double using the Double.longBitsToDouble method:如果你想要一个double任何double ,特定范围之外,则可以采取的一个事实优点double由64位表示,并且可以任何64位值转换为一个doubleDouble.longBitsToDouble方法:

public static void main(String[] args) {
    Random r = new Random();

    double d;
    do {
        d = Double.longBitsToDouble(r.nextLong());
    } while (d >= 20 && d <= 50);

    System.out.println(d);
}

First of all you always need some upper bound for the number you're generating, so 'above rangeMax' won't really do.首先,你总是需要一些你正在生成的数字的上限,所以 'above rangeMax' 不会真正做到。 What you basically want is to have a number generated that falls into one of two ranges [0,minRange] or [maxRange, maxValue].您基本上想要的是生成一个落入两个范围 [0,minRange] 或 [maxRange, maxValue] 之一的数字。

You can either go with the 'lazy approach' which is just generating a value between 0 and maxValue and generate a new one until you get on that does not fall into the [minRange,maxRange] range or you could do a two step generation process, ie generate a random number that determines whether you generate a number in the lower range or the upper range, for instance:您可以使用“懒惰方法”,它只是生成一个介于 0 和 maxValue 之间的值,然后生成一个新的值,直到您继续使用它不落入 [minRange,maxRange] 范围内,或者您可以执行两步生成过程, 即生成一个随机数,决定您生成的是下限还是上限的数字,例如:

public static void main(String[] args) {
    double result  = (new Random().nextInt(2)) == 0 ? generateInRange(0, 20) : generateInRange(50, Double.MAX_VALUE);
}

private static double generateInRange(double min, double max) {
    return new Random().nextDouble() * (max-min) + min;
}

This does give you a 50/50 chance of ending up in the lower and upper range, so you might want to tweak that.这确实为您提供了 50/50 的机会在下限和上限范围内结束,因此您可能需要对其进行调整。

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

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