为什么不(int) Math.random()*10在Java中生成10 ?

时间:2021-02-07 13:31:46

Why the following produce between 0 - 9 and not 10?

为什么是0 - 9而不是10?

My understanding is Math.random() create number between 0 to under 1.0.

我的理解是Math.random()在0到1.0之间创建数字。

So it can produce 0.99987 which becomes 10 by *10, isn't it?

它可以产生0。99987,变成10×10,对吧?

int targetNumber = (int) (Math.random()* 10);

8 个解决方案

#1


17  

Casting a double to an int in Java does integer truncation. This means that if your random number is 0.99987, then multiplying by 10 gives 9.9987, and integer truncation gives 9.

在Java中,将double转换为整数,可以进行整数截断。这意味着如果你的随机数是0。99987,那么乘以10得到9。9987,整数截断得到9。

#2


14  

From the Math javadoc :

来自数学javadoc:

"a pseudorandom double greater than or equal to 0.0 and less than 1.0"

“伪随机数倍大于或等于0.0,小于1.0”

1.0 is not a posible value with Math.random. So you can't obtain 10. And (int) 9.999 gives 9

1。0和Math.random是不一致的。所以不能得到10。9。999等于9。

#3


2  

Cause (Math.random()* 10 gets rounded down using int(), so int(9.9999999) yields 9.

原因(Math.random()* 10使用int()得到整数,所以int(9.9999999)得到9。

#4


1  

Because (int) rounds down.
(int)9.999 results in 9. //integer truncation

因为(int)轮。(int)9.999 9。/ /整数截断

#5


0  

0.99987 which becomes 10 by *10

0。99987变成10 *10。

Not when I went to school. It becomes 9.9987.

当我去学校的时候。它变成了9.9987。

#6


0  

Math.floor(Math.random() * 10) + 1

Math.floor(Math.random() * 10) + 1。

Now you get a integer number between 1 and 10, including the number 10.

现在你得到一个整数在1到10之间,包括数字10。

#7


0  

You could always tack on a +1 to the end of the command, allowing it to add 1 to the generated number. So, you would have something like this:

您可以在命令的末尾添加一个+1,允许它将1添加到生成的数字中。所以,你会得到这样的东西:

int randomnumber = ( (int)(Math.random( )*10) +1);

This would generate any integer between 1 and 10.

这将生成1到10之间的任何整数。

If you wanted any integer between 0 and 10, you could do this:

如果你想要0到10之间的任何整数,你可以这样做:

int randomnumber = ( (int)(Math.random( )*11) -1);

Hope this helps!

希望这可以帮助!

#8


0  

you can add 1 to the equation so the output will be from 1 to 10 instead of 0 to 9 like this :

你可以把1加到等式中,结果是1到10,而不是0到9

int targetNumber = (int) (Math.random()* 10+1);

#1


17  

Casting a double to an int in Java does integer truncation. This means that if your random number is 0.99987, then multiplying by 10 gives 9.9987, and integer truncation gives 9.

在Java中,将double转换为整数,可以进行整数截断。这意味着如果你的随机数是0。99987,那么乘以10得到9。9987,整数截断得到9。

#2


14  

From the Math javadoc :

来自数学javadoc:

"a pseudorandom double greater than or equal to 0.0 and less than 1.0"

“伪随机数倍大于或等于0.0,小于1.0”

1.0 is not a posible value with Math.random. So you can't obtain 10. And (int) 9.999 gives 9

1。0和Math.random是不一致的。所以不能得到10。9。999等于9。

#3


2  

Cause (Math.random()* 10 gets rounded down using int(), so int(9.9999999) yields 9.

原因(Math.random()* 10使用int()得到整数,所以int(9.9999999)得到9。

#4


1  

Because (int) rounds down.
(int)9.999 results in 9. //integer truncation

因为(int)轮。(int)9.999 9。/ /整数截断

#5


0  

0.99987 which becomes 10 by *10

0。99987变成10 *10。

Not when I went to school. It becomes 9.9987.

当我去学校的时候。它变成了9.9987。

#6


0  

Math.floor(Math.random() * 10) + 1

Math.floor(Math.random() * 10) + 1。

Now you get a integer number between 1 and 10, including the number 10.

现在你得到一个整数在1到10之间,包括数字10。

#7


0  

You could always tack on a +1 to the end of the command, allowing it to add 1 to the generated number. So, you would have something like this:

您可以在命令的末尾添加一个+1,允许它将1添加到生成的数字中。所以,你会得到这样的东西:

int randomnumber = ( (int)(Math.random( )*10) +1);

This would generate any integer between 1 and 10.

这将生成1到10之间的任何整数。

If you wanted any integer between 0 and 10, you could do this:

如果你想要0到10之间的任何整数,你可以这样做:

int randomnumber = ( (int)(Math.random( )*11) -1);

Hope this helps!

希望这可以帮助!

#8


0  

you can add 1 to the equation so the output will be from 1 to 10 instead of 0 to 9 like this :

你可以把1加到等式中,结果是1到10,而不是0到9

int targetNumber = (int) (Math.random()* 10+1);