1.8 随机数总结

Math.random()

MDN 文档中是这样介绍的:Math.random()函数返回0-1范围内的浮点伪随机数包括0,但不包括1),在该范围内具有近似均匀的分布 - 然后可以缩放到所需范围。该实现选择初始种子到随机数生成算法; 它不能被用户选择或重置。

随机生成10以内任意长度的随机数

function getRandomInt(len) {
 let arr = Array.from({length:len},() => Math.random()*10|0);
 console.log(arr)
}

获取两个值之间的随机整数,包括

function getRandomIntInclusive(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min; //The maximum is inclusive and the minimum is inclusive 
}

 

猜你喜欢

转载自www.cnblogs.com/-constructor/p/10732908.html
1.8
今日推荐