Javascript 了解Math.floor在随机访问数组时的用法

Javascript 了解Math.floor在随机访问数组时的用法,javascript,arrays,math,random,floor,Javascript,Arrays,Math,Random,Floor,我使用在这个网站上找到的代码访问字符串值数组。我更改了变量名,但除此之外,代码保持不变 var rand = array[Math.floor(Math.random() * array.length)]; 它是有效的,据我所知,(Math.random()*array.length)是生成随机数本身的区域,那么为什么需要Math.floor?很明显,我不理解这里的一些非常明显的东西。因为没有分数数组元素 const数组=[123456]; log(数组[0.5]),因为没有分数数组元素

我使用在这个网站上找到的代码访问字符串值数组。我更改了变量名,但除此之外,代码保持不变

var rand = array[Math.floor(Math.random() * array.length)];

它是有效的,据我所知,
(Math.random()*array.length)
是生成随机数本身的区域,那么为什么需要
Math.floor
?很明显,我不理解这里的一些非常明显的东西。

因为没有分数数组元素

const数组=[123456];

log(数组[0.5]),因为没有分数数组元素

const数组=[123456];

log(数组[0.5])
Math.floor
返回一个整数,而
Math.random()
将返回一个介于0和1之间的浮点值

要访问数组中的项目,如
rand[0]
,您需要有一个整数。您无法使用
rand[1.43]
访问数组中的项


这一小行代码将访问数组中的一个随机项,方法是生成一个从零到数组长度的随机浮点,并使用
Math.floor
Math.floor
返回一个整数,而
Math.random()
将返回一个介于0和1之间的浮点

要访问数组中的项目,如
rand[0]
,您需要有一个整数。您无法使用
rand[1.43]
访问数组中的项

这一小行代码将通过生成一个从零到数组长度的随机浮点,并使用Math.floor将其四舍五入到最接近的整数来访问数组中的随机项
var数组=[10,6,2,11,0];
console.log(数组[3]);//输出=11
//Math.random()生成从0(包含)到1(排除)的随机浮点数。
//Math.random()*array.length将生成从0到array.length(独占)的所有随机数
var randomNumber=Math.random();
console.log(“randomNumber=“+randomNumber);//比如,输出=0.8604458676303853
console.log(“randomNumber*array.length=“+randomNumber*array.length”);//比如,输出=4.3022293381519265
//要访问数组中的任何元素,需要从0到array.length(独占)的索引
//var rand=array[randomNumber*array.length];
//将给出错误,因为数组无法访问浮点索引
console.log(“Math.floor(randomNumber*array.length)=”+Math.floor(randomNumber*array.length));//比如说,输出=4
//最后,,
var rand=array[Math.floor(Math.random()*array.length)];
console.log(“rand=”,rand)
var数组=[10,6,2,11,0];
console.log(数组[3]);//输出=11
//Math.random()生成从0(包含)到1(排除)的随机浮点数。
//Math.random()*array.length将生成从0到array.length(独占)的所有随机数
var randomNumber=Math.random();
console.log(“randomNumber=“+randomNumber);//比如,输出=0.8604458676303853
console.log(“randomNumber*array.length=“+randomNumber*array.length”);//比如,输出=4.3022293381519265
//要访问数组中的任何元素,需要从0到array.length(独占)的索引
//var rand=array[randomNumber*array.length];
//将给出错误,因为数组无法访问浮点索引
console.log(“Math.floor(randomNumber*array.length)=”+Math.floor(randomNumber*array.length));//比如说,输出=4
//最后,,
var rand=array[Math.floor(Math.random()*array.length)];

console.log(“rand=”,rand)
Math.random
返回一个浮点数,数组元素索引实际上是整数
Math.floor
将浮点向下舍入到最接近的整数。
Math.random
返回一个浮点数,数组元素索引实际上是整数
Math.floor
将浮点向下舍入到最接近的整数。从不知道
|0
。谢谢从不知道
|0
。谢谢因此,
Math.random()
可能会产生0.7,乘以
array.length
(在我的例子中为3),将产生2.1,这将
Math.floor
设为2,这意味着将访问第三个数组值。对吗?对!这是最基本的,谢谢你。所有的回答都很好,超出了我的要求,但你的解释很容易理解:)因此,
Math.random()
可能会产生0.7,乘以数组。长度(我的例子中是3),将得到2.1,这将是2,这意味着将访问第三个数组值。对吗?对!这是最基本的,谢谢你。所有的回答都很好,超出了我的要求,但你的解释方式让我很容易理解:)对于你的第二点,Math.floor不做第二件事,Math.random不返回1。所有的地板都是四舍五入的。为使点1清晰,地板不会“环绕”it地板。你会打电话过来的。另一种方法是把这个
Math.ceil
向上取整,
Math.floor
向下取整,
Math.round
向上取整。谢谢你纠正我。我脑子里想的是,但我不小心把它写错了。对于你的第二点,Math.floor不能做第二件事,Math.random通过不返回1来做到这一点。所有的地板都是四舍五入的。为使点1清晰,地板不会“环绕”it地板。你会打电话过来的。另一种方法是把这个
Math.ceil
向上取整,
Math.floor
向下取整,
Math.round
向上取整。谢谢你纠正我。我心里有这个想法,但我不小心把它写错了。