Java 如何获得范围内带有负数的随机数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27976857/
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
How to get random number with negative number in range?
提问by CaffeineToCode
Consider the following code:
考虑以下代码:
int rand = new Random().nextInt((30 - 20) + 1) + 20
It will return a random number between 30 and 20. However, I need its range to include negative numbers. How would I include negative numbers in the generation?I have tried using math that would be negative, but that resulted in an error. Simply subtracting or adding the negative numbers would not yield the desired value.
它将返回 30 到 20 之间的随机数。但是,我需要它的范围来包含负数。我将如何在世代中包含负数?我曾尝试使用会是负数的数学方法,但这导致了错误。简单地减去或添加负数不会产生所需的值。
EDIT:Sorry, I am only half awake. The correct code is int rand = new Random().nextInt((30 - 20) + 1) + 20;
.
编辑:对不起,我只是半醒。正确的代码是int rand = new Random().nextInt((30 - 20) + 1) + 20;
。
采纳答案by JClassic
To get random number between a set range with min
and max
:
要使用min
和获取设置范围之间的随机数max
:
int number = random.nextInt(max - min) + min;
Also works with negative numbers
也适用于负数
So:
所以:
random.nextInt(30 + 10) - 10;
// max = 30; min = -10;
Will yield a random int between -10 and 30.(exclusive)
将产生 -10 到 30 之间的随机整数。(不包括)
Also works with doubles
也适用于双打
回答by Elliott Frisch
Okay. First, try to only create Random
instance once, but for an example
好的。首先,尝试只创建Random
一次实例,但举个例子
int rand = -15 + new Random().nextInt(31);
is the range -15 to 15. The Random.nextInt(int)
JavaDoc says (in part) Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive). Note that your provided example of (30 - 20) + 1
is the range 0 to 10 (inclusive).
是 -15 到 15 的范围Random.nextInt(int)
。JavaDoc 说(部分)返回一个伪随机、均匀分布在 0(包括)和指定值(不包括在内)之间的 int 值。请注意,您提供的示例(30 - 20) + 1
范围是 0 到 10(含)。
Edit
编辑
as a further example, to get the range 20 to 30 you would
再举一个例子,要获得 20 到 30 的范围,你会
int rand = 20 + new Random().nextInt(11);
Remember, the bounds of the result of is 0 to n
.
请记住, 的结果的边界是 0 到n
。
Edit 2
编辑 2
30 - -10. I'm trying to make a survival simulator. That will be my temperature.
30 - -10。我正在尝试制作生存模拟器。那将是我的体温。
Ok. Let's write that range as -10
to 30
. nextInt(n)
will return a value between 0
and n
so if you want the range below 0 you must subtract 10
from the result and add 10
to the n
. That's
好的。让我们编写范围内-10
对30
。nextInt(n)
将返回一个介于0
和之间的值,n
因此如果您希望范围低于 0,则必须10
从结果中减去并添加10
到n
. 那是
Random random = new Random();
int rand = random.nextInt(41) - 10;
Now let's examine how we can determine those numbers. Remember, nextInt()
will return between 0
and n
(exclusive) and we want -10
to 30
(inclusive); so 41
is n
and we subtract 10
. If the result is 0
(the min) we get -10
, if the result is 40
(the max) we get 30
.
现在让我们研究如何确定这些数字。请记住,nextInt()
将返回之间0
和n
(独家),我们希望-10
到30
(含); 所以41
是n
,我们减去10
。如果结果是0
(最小值)我们得到-10
,如果结果是40
(最大值)我们得到30
。
回答by jnd
int randomNumber=(random.nextInt(DOUBLE_MAX)-MAX);
Alternatively, create a random positive or negative 1 and multiply it.
或者,创建一个随机的正数或负数 1 并将其相乘。
回答by KennyC
You can use Random.nextBoolean() to determine if its a random positive or negative int.
您可以使用 Random.nextBoolean() 来确定它是随机的正整数还是负整数。
int MAX = 30;
Random random = new Random(); //optionally, you can specify a seed, e.g. timestamp.
int rand = random.nextInt(MAX) * (random .nextBoolean() ? -1 : 1);