Javascript 为什么我要把 Math.floor 和 Math.random 结合起来?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8002820/
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
Why would I combine Math.floor with Math.random?
提问by Aziz Al-ghannam
Why would anybody call Math.floor
on a Math.random
result? I've seen it used like:
为什么会有人呼吁Math.floor
对Math.random
结果呢?我见过它像这样使用:
Math.floor(Math.random() * num);
Can someone explain please?
有人可以解释一下吗?
回答by Mark Byers
Math.random
returns a floating-point numberbetween 0 and 1.
Math.random
返回一个介于 0 和 1 之间的浮点数。
Returns a floating-point, pseudo-random number in the range [0, 1) that is, from 0 (inclusive) up to but not including 1 (exclusive), which you can then scale to your desired range.
返回 [0, 1) 范围内的浮点伪随机数,即从 0(含)到但不包括 1(不含),然后您可以将其缩放到所需的范围。
Multiplying this by n gives a floating point number between 0 (inclusive) and n (exclusive).
将其乘以 n 给出一个介于 0(含)和 n(不含)之间的浮点数。
Math.floor
is then used to convert this floating point number to an integerbetween 0 and n - 1 (inclusive).
Math.floor
然后用于将此浮点数转换为 0 到 n - 1(含)之间的整数。
回答by jfriend00
Why would anybody call Math.floor on a Math.random result?
为什么有人会在 Math.random 结果上调用 Math.floor?
In a nutshell, one calls Math.floor()
when you want to truncate a decimal value to its nearest integer (by just dropping the decimal portion. So, 3.9 becomes 3, 2.1 becomes 2, etc... So, you would typically use that when you need an integer and you want the integer that is smaller than or equal to the decimal value. The math library also has Math.ceil()
and Math.round()
. Math.ceil()
gets you the next larger integer, Math.round()
rounds to the nearest integer going either up or down depending upon which is closer.
简而言之,Math.floor()
当您想将十进制值截断为其最接近的整数时(通过删除小数部分。因此,3.9 变为 3,2.1 变为 2,等等......所以,您通常会在需要时使用它一个整数,你想要小于或等于十进制值的整数。数学库也有Math.ceil()
和Math.round()
. 为 Math.ceil()
你提供下一个更大的整数,Math.round()
四舍五入到最接近的整数,取决于哪个更接近。
I've seen it used like:
Math.floor(Math.random() * num);
我见过它像这样使用:
Math.floor(Math.random() * num);
Breaking Math.floor(Math.Random() * num)
down into it's individual pieces and explaining each piece, you get this:
打破Math.floor(Math.Random() * num)
下来到它的各个部分,并解释每一块,你会得到这样的:
Math.random()
gives you a random decimal number between 0 and 1, including 0, but not including 1. So, it might give you something like 0.38548569372
.
Math.random()
为您提供 0 到 1 之间的随机十进制数,包括 0,但不包括 1。因此,它可能会给您类似0.38548569372
.
Math.random() * num
gives you a random decimal number between 0 and num, including 0, but not including num. So, if num was 10, it might give you 3.8548569372
.
Math.random() * num
为您提供 0 和 num 之间的随机十进制数,包括 0,但不包括 num。所以,如果 num 是 10,它可能会给你3.8548569372
.
Math.floor(Math.random() * num))
gives you a random integer number between 0 and num, including 0, but not including num. So, it might give you 3
.
Math.floor(Math.random() * num))
给你一个 0 到 num 之间的随机整数,包括 0,但不包括 num。所以,它可能会给你3
。
Math.floor()
truncates the decimal number to only the integer portion. A random integer is often used for getting a random value from an array (which needs to be an integer).
Math.floor()
将十进制数截断为仅整数部分。随机整数通常用于从数组(必须是整数)中获取随机值。
回答by JCOC611
Math.random()
will give you a long, random decimal. What one usually does is multiply that decimal by 10, 100, 1000, etc to get a random whole number. However, since such decimal is so long, to get a absolute whole number, you use Math.floor()
to round that number down.
Math.random()
会给你一个长的、随机的小数点。人们通常做的是将小数乘以 10、100、1000 等以得到一个随机整数。但是,由于这样的小数很长,要获得绝对整数,您Math.floor()
需要将该数字四舍五入。
回答by Freesn?w
Why would I combine Math.floor
With Math.random
?
我为什么要结合Math.floor
With Math.random
?
You combine them because otherwise it would return a float. Using Math.floor makes sure that it is a whole number inside of the range specified.
您将它们组合在一起,否则它会返回一个浮点数。使用 Math.floor 确保它是指定范围内的整数。
Math.random returns a flat in between 0 and 1. Multiplying it by your num
or max range gets you a value with a max of that number (1 * num
). So again, Math.floor is just forcing it to be a whole number.
Math.random 返回一个介于 0 和 1 之间的平坦值。将它乘以您的num
或最大范围会得到一个最大值为该数字 (1 * num
) 的值。同样,Math.floor 只是强迫它是一个整数。
Behind The Scenes:
幕后花絮:
RANDOM NUMBER -> .35 -> Multiplied by max (num
) of 11 -> Gets 3.85 -> Math.floor(3.85) -> 3.
随机数 -> .35 -> 乘以 max ( num
) of 11 -> 得到 3.85 -> Math.floor(3.85) -> 3。
Keep in mind, num
is the MAX + 1. Setting num
to 5 will only generate numbers 1-4!
记住,num
是MAX+1。设置num
为 5 只会生成数字 1-4!
You can check out this link for more information: http://www.javascriptkit.com/javatutors/randomnum.shtml
您可以查看此链接以获取更多信息:http: //www.javascriptkit.com/javatutors/randomnum.shtml
Tada :)
多田:)
回答by switz
Math.random()
returns something like 0.8747230430599302
between [0,1)
Math.random()
返回类似于0.8747230430599302
[0,1) 之间的内容
We use .floor
to round it down to the nearest integer. For example:
我们使用.floor
将其四舍五入到最接近的整数。例如:
Math.random()*5 == 2.5889716914389282
This generates a number between [0,5).
Math.random()*5 == 2.5889716914389282
这会生成一个介于 [0,5) 之间的数字。
Math.floor(Math.random()*5) == 2 //in this scenario
Generates a number between [0,4]
Math.floor(Math.random()*5) == 2 //in this scenario
生成 [0,4] 之间的数字
回答by gman
It's used to get an integer random number between 0 and (max - 1).
它用于获取 0 到 (max - 1) 之间的整数随机数。
On the other hand it's faster to use | 0
as in
在另一方面,它的速度更快,使用| 0
作为
const randomInt = Math.random() * num | 0;
The | 0
is a binary orof 0 which the JavaScript spec effectively says the result is converted to an integer before the |
happens. Note that | 0
is not the same as Math.floor
. | 0
rounds to 0 whereas Math.floor
rounds down.
这| 0
是一个二进制或0,JavaScript 规范有效地表示结果在|
发生之前被转换为整数。请注意,| 0
这与Math.floor
. | 0
舍入为 0 而Math.floor
向下舍入。
| 0 Math.floor
------+------+------------
2.5 | 2 | 2
1.5 | 1 | 1
0.5 | 0 | 0
-0.5 | 0 | -1
-1.5 | -1 | -2
-2.5 | -2 | -3
回答by Alireza
var num = Math.floor(Math.random() * 1000); // e.g. 885
var flNum = Math.random() * 1000; //e.g. 885.9936205333221
Try Math.random() * 1000 for example, you may get something like this: 885.9936205333221, in many cases we need a rounded number, so many developers use it with Math.floor or Math.ceil to get an integer like 885, if in your case, you don't mind having a float number, leave it as it's...
以 Math.random() * 1000 为例,你可能会得到这样的结果:885.9936205333221,在很多情况下我们需要一个四舍五入的数字,所以很多开发者用它和 Math.floor 或 Math.ceil 来得到一个像 885 这样的整数,如果在你的情况下,你不介意有一个浮点数,保持原样......
For more info about how Math.floor working, check this link:
有关 Math.floor 如何工作的更多信息,请查看此链接:
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Math/floor
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Math/floor