Math.floor((Math.random()* 10)+ 1);工作? [英] How does Math.floor( (Math.random() * 10) + 1 ); work?

查看:191
本文介绍了Math.floor((Math.random()* 10)+ 1);工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解Math.floor将数字四舍五入到最小整数,但是当我们这样做时:Math.random()* 10不会将数字乘以10,例如9 * 10 = 90,所以数字如何在1到10之间?

I understand that Math.floor rounds the number to the lowest integer but when we do this: Math.random() * 10 wouldn't it multiply the number by 10 for example 9 * 10 = 90 so how would the number be between 1 and 10?

感谢您帮助我获得了答案!

Thank you for helping I got the answer!

推荐答案

Math.random()从[ 0 1 )(浮点数:'['=包含在内,')'=排除).

Math.random() provides a random number from [0,1) (floating point: '[' = inclusive, ')' = exclusive).

因此在 Math.floor((Math.random()* 10)+ 1); 中将 Math.random()乘以 10 将从[ 0 10 )中提供一个随机数.

So in Math.floor( (Math.random() * 10) + 1); multiplying Math.random() by 10 will provide a random number from [0, 10).

乘法后的 +1 会将输出更改为[ 1 11 ).

The +1 after the multiplication will change the output to be [1, 11).

然后,最后 Math.floor(...)将范围为[ 1 11 )的随机数转换为一个整数值.

Then finally Math.floor( ... ) converts the random number that is in the range from [1, 11) to an integer value.

因此,已执行语句的范围将是[ 1 10 ]中的所有整数.或者更具体地说,它将是该集合中的数字之一:{ 1 2 3 4 5 6 7 8 9 10 }

So the range of the executed statement will be all integers from [1, 10]. Or to be more specific it will be one of the numbers in this set: { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }

这篇关于Math.floor((Math.random()* 10)+ 1);工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆