了解随机访问数组时 Math.floor 的用法
Understanding the use of Math.floor when randomly accessing an array
我使用了在此站点上找到的代码来访问字符串值数组。我更改了变量名,但除此之外,代码保持不变。
var rand = array[Math.floor(Math.random() * array.length)];
有效,据我了解,(Math.random() * array.length)
是生成随机数的区域本身,那么为什么需要Math.floor
?我显然不理解这里很明显的东西。
因为没有小数数组元素
const array = [123, 456];
console.log(array[0.5]);
上面的代码打印 undefined
因为没有 0.5
元素。有一个 0
元素和一个 1
元素但没有 0.5 元素
因此,要选择 Math.random()
的随机元素,其中 returns 介于 0 和 1 之间(但不是 1)的数字,您必须转换为整数。
JavaScript 数组与大多数其他语言中的数组不同。它们实际上更像是增强对象。它们的作用类似于数组,因为您可以获得长度 (array.length
) 并调用诸如 push、pop、concat、shift、unshift 之类的东西,但您还可以添加属性
array = [4, 5, 6];
array["foo"] = "bar"
console.log(Object.keys(array));
版画
[
"0",
"1",
"2",
"foo"
]
这解释了为什么没有 Math.floor
的 Math.random
会失败。从 JavaScript 的语义角度来看,数组只是一个具有名为 0、1、2、3 等属性的对象。当然,在幕后,这不是 JavaScript 所做的,因为它太慢了以这种方式实现数组,但在大多数情况下,数组的行为就像它们实际上只是一个具有名为“0”、“1”、“2”的属性的对象,并且具有名为“0.00012”等的属性是完全有效的
注意,这样做实际上会更快
const rand = array[Math.random() * array.length | 0];
| 0
是 0 的二进制 或 ,JavaScript 规范有效地表示结果在 [=24= 之前转换为整数] 发生。请注意 | 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
Math.floor
return 是一个整数,而 Math.random()
将 return 是一个介于 0 和 1 之间的浮点数。
要访问数组中的项目,例如 rand[0]
,您需要有一个整数。您无法使用 rand[1.43]
.
访问数组中的项目
这一行代码将生成一个从零到数组长度的随机浮点数,然后用 Math.floor
[=15= 将其四舍五入到最接近的整数,从而访问数组中的随机项]
var array = [10,6,2,11,0];
console.log(array[3]); // output = 11
//Math.random() generates random floating point numbers from 0(inclusive) to 1(exclusive).
//Math.random() * array.length will generate all random numbers from 0 to array.length(exclusive)
var randomNumber = Math.random();
console.log("randomNumber = " + randomNumber); // Say, output = 0.8604458676303853
console.log("randomNumber * array.length = " + randomNumber * array.length); // Say, output = 4.3022293381519265
//To access any element in the array you will need indices starting from 0 to array.length(exclusive)
//var rand = array[randomNumber * array.length];
//Will give error as array cannot access a floating point index
console.log("Math.floor(randomNumber * array.length) = " + Math.floor(randomNumber * array.length)); // Say, output = 4
//Finally,
var rand = array[Math.floor(Math.random() * array.length)];
console.log("rand = ", rand);
floor 函数将为您做以下事情。
- 它会首先舍入浮点数,以便它是一个有效的索引来访问。
总体而言,Math.floor(Math.random() * array.length)
会注意最大索引值始终为 array.length - 1
原因是我们得到的最大数量总是略小于array.length)。
所以即使 Math.random() 生成 0.999 并且你将它乘以 array.length = 5。
因此,0.999 * 5 = 4.995 和 (4.995) = 4 的下限。满足约束:)
我使用了在此站点上找到的代码来访问字符串值数组。我更改了变量名,但除此之外,代码保持不变。
var rand = array[Math.floor(Math.random() * array.length)];
有效,据我了解,(Math.random() * array.length)
是生成随机数的区域本身,那么为什么需要Math.floor
?我显然不理解这里很明显的东西。
因为没有小数数组元素
const array = [123, 456];
console.log(array[0.5]);
上面的代码打印 undefined
因为没有 0.5
元素。有一个 0
元素和一个 1
元素但没有 0.5 元素
因此,要选择 Math.random()
的随机元素,其中 returns 介于 0 和 1 之间(但不是 1)的数字,您必须转换为整数。
JavaScript 数组与大多数其他语言中的数组不同。它们实际上更像是增强对象。它们的作用类似于数组,因为您可以获得长度 (array.length
) 并调用诸如 push、pop、concat、shift、unshift 之类的东西,但您还可以添加属性
array = [4, 5, 6];
array["foo"] = "bar"
console.log(Object.keys(array));
版画
[
"0",
"1",
"2",
"foo"
]
这解释了为什么没有 Math.floor
的 Math.random
会失败。从 JavaScript 的语义角度来看,数组只是一个具有名为 0、1、2、3 等属性的对象。当然,在幕后,这不是 JavaScript 所做的,因为它太慢了以这种方式实现数组,但在大多数情况下,数组的行为就像它们实际上只是一个具有名为“0”、“1”、“2”的属性的对象,并且具有名为“0.00012”等的属性是完全有效的
注意,这样做实际上会更快
const rand = array[Math.random() * array.length | 0];
| 0
是 0 的二进制 或 ,JavaScript 规范有效地表示结果在 [=24= 之前转换为整数] 发生。请注意 | 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
Math.floor
return 是一个整数,而 Math.random()
将 return 是一个介于 0 和 1 之间的浮点数。
要访问数组中的项目,例如 rand[0]
,您需要有一个整数。您无法使用 rand[1.43]
.
这一行代码将生成一个从零到数组长度的随机浮点数,然后用 Math.floor
[=15= 将其四舍五入到最接近的整数,从而访问数组中的随机项]
var array = [10,6,2,11,0];
console.log(array[3]); // output = 11
//Math.random() generates random floating point numbers from 0(inclusive) to 1(exclusive).
//Math.random() * array.length will generate all random numbers from 0 to array.length(exclusive)
var randomNumber = Math.random();
console.log("randomNumber = " + randomNumber); // Say, output = 0.8604458676303853
console.log("randomNumber * array.length = " + randomNumber * array.length); // Say, output = 4.3022293381519265
//To access any element in the array you will need indices starting from 0 to array.length(exclusive)
//var rand = array[randomNumber * array.length];
//Will give error as array cannot access a floating point index
console.log("Math.floor(randomNumber * array.length) = " + Math.floor(randomNumber * array.length)); // Say, output = 4
//Finally,
var rand = array[Math.floor(Math.random() * array.length)];
console.log("rand = ", rand);
floor 函数将为您做以下事情。
- 它会首先舍入浮点数,以便它是一个有效的索引来访问。
总体而言,Math.floor(Math.random() * array.length)
会注意最大索引值始终为 array.length - 1
原因是我们得到的最大数量总是略小于array.length)。
所以即使 Math.random() 生成 0.999 并且你将它乘以 array.length = 5。
因此,0.999 * 5 = 4.995 和 (4.995) = 4 的下限。满足约束:)