Java 生成一个范围内的随机双精度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3680637/
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
Generate a random double in a range
提问by swati
I have two doubles like the following
我有两个像下面这样的双打
double min = 100;
double max = 101;
and with a random generator, I need to create a double value between the range of min and max.
使用随机生成器,我需要在最小值和最大值之间创建一个双精度值。
Random r = new Random();
r.nextDouble();
but there is nothing here where we can specify the range.
但是这里没有任何东西可以指定范围。
回答by mob
To generate a random value between rangeMin
and rangeMax
:
在rangeMin
和之间生成一个随机值rangeMax
:
Random r = new Random();
double randomValue = rangeMin + (rangeMax - rangeMin) * r.nextDouble();
回答by Topera
Use this:
用这个:
double start = 400;
double end = 402;
double random = new Random().nextDouble();
double result = start + (random * (end - start));
System.out.println(result);
EDIT:
编辑:
new Random().nextDouble()
: randomly generates a number between 0 and 1.
new Random().nextDouble()
: 随机生成一个 0 到 1 之间的数字。
start
: start number, to shift number "to the right"
start
: 起始编号,将编号“向右”移动
end - start
: interval. Random gives you from 0% to 100% of this number, because random gives you a number from 0 to 1.
end - start
: 间隔。随机给你这个数字的 0% 到 100%,因为随机给你一个从 0 到 1 的数字。
EDIT 2:Tks @daniel and @aaa bbb. My first answer was wrong.
编辑 2:Tks @daniel 和 @aaa bbb。我的第一个答案是错误的。
回答by Tunaki
This question was asked before Java 7 release but now, there is another possible way using Java 7 (and above) API:
这个问题是在 Java 7 发布之前提出的,但现在,还有另一种使用 Java 7(及更高版本)API 的可能方法:
double random = ThreadLocalRandom.current().nextDouble(min, max);
nextDouble
will return a pseudorandom double value between the minimum (inclusive) and the maximum (exclusive). The bounds are not necessarily int
, and can be double
.
nextDouble
将返回最小值(包含)和最大值(不包含)之间的伪随机双精度值。界限不一定是int
,也可以是double
。
回答by Larson Carter
import java.util.Random;
public class MyClass {
public static void main(String args[]) {
Double min = 0.0; // Set To Your Desired Min Value
Double max = 10.0; // Set To Your Desired Max Value
double x = (Math.random() * ((max - min) + 1)) + min; // This Will Create A Random Number Inbetween Your Min And Max.
double xrounded = Math.round(x * 100.0) / 100.0; // Creates Answer To The Nearest 100 th, You Can Modify This To Change How It Rounds.
System.out.println(xrounded); // This Will Now Print Out The Rounded, Random Number.
}
}
回答by Tau
Random random = new Random();
double percent = 10.0; //10.0%
if (random.nextDouble() * 100D < percent) {
//do
}
回答by Yair Landmann
The main idea of random is that it returns a pseudorandom value. There is no such thing as fully random functions, hence, 2 Random instances using the same seed will return the same value in certain conditions.
random 的主要思想是它返回一个伪随机值。没有完全随机函数这样的东西,因此,使用相同种子的 2 个 Random 实例在某些条件下将返回相同的值。
It is a good practice to first view the function doc in order to understand it (https://docs.oracle.com/javase/8/docs/api/java/util/Random.html)
最好先查看函数文档以了解它(https://docs.oracle.com/javase/8/docs/api/java/util/Random.html)
Now that we understand that the returned value of the function nextDouble() is a pseudorandom value between 0.0 and 1.0 we can use it to our advantage.
现在我们知道函数 nextDouble() 的返回值是一个介于 0.0 和 1.0 之间的伪随机值,我们可以利用它来发挥我们的优势。
For creating a random number between A and B givin' that the boundaries are valid (A>B) we need to: 1. find the range between A and B so we can know how to many "steps" we have. 2. use the random function to determine how many steps to take (because the returned value is between 0.0 and 1.0 you can think of it as "pick a random percentage of increase" 3. add the offset
为了在 A 和 B 之间创建一个随机数,假设边界是有效的 (A>B),我们需要: 1. 找到 A 和 B 之间的范围,这样我们就可以知道我们有多少“步骤”。2. 使用随机函数来确定要走多少步(因为返回值在 0.0 和 1.0 之间,您可以将其视为“选择随机增加的百分比” 3. 添加偏移量
After all of that, you can see that mob gave you the easiest and most common way to do so in my opinion
毕竟,你可以看到,在我看来,mob 给了你最简单和最常见的方法
double randomValue = rangeMin + (rangeMax - rangeMin) * r.nextDouble();
double randomValue = rangeMin + (rangeMax - rangeMin) * r.nextDouble();
double RandomValue = Offset + (Range)*(randomVal between 0.0-1.0)
double RandomValue = Offset + (Range)*(randomVal 在 0.0-1.0 之间)