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

时间:2022-11-25 20:14:46

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 e.g

我会要求一个超出范围的人,例如

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?

有人可以建议吗?

4 个解决方案

#1


0  

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

你生成一个随机的double(需要乘以100,因为生成0和1之间的双返回值)和循环,而结果> 20和<50

#2


2  

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.

它不是资源昂贵且易于理解。

#3


0  

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位表示的事实,你可以使用Double.longBitsToDouble方法将任何64位值转换为double。 :

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);
}

#4


-1  

First of all you always need some upper bound for the number you're generating, so 'above rangeMax' won't really do. What you basically want is to have a number generated that falls into one of two ranges [0,minRange] or [maxRange, maxValue].

首先,你总是需要为你生成的数字设置一个上界,所以'rangeMax'以上就不会真正做到。您基本上想要的是生成的数字落入两个范围[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, i.e. 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的机会在最低和最高范围内结束,所以你可能想要调整它。

#1


0  

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

你生成一个随机的double(需要乘以100,因为生成0和1之间的双返回值)和循环,而结果> 20和<50

#2


2  

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.

它不是资源昂贵且易于理解。

#3


0  

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位表示的事实,你可以使用Double.longBitsToDouble方法将任何64位值转换为double。 :

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);
}

#4


-1  

First of all you always need some upper bound for the number you're generating, so 'above rangeMax' won't really do. What you basically want is to have a number generated that falls into one of two ranges [0,minRange] or [maxRange, maxValue].

首先,你总是需要为你生成的数字设置一个上界,所以'rangeMax'以上就不会真正做到。您基本上想要的是生成的数字落入两个范围[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, i.e. 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的机会在最低和最高范围内结束,所以你可能想要调整它。