生成一个随机数
- 函数:
Math.random(),返回[0,1)
let num = Math.random()
console.log(num)
生成一个指定范围内的随机数
let min = 4
let max = 5
let num1 = Math.random() * (max - min + 1) + min //公式
console.log(num1)
// let num2 = Math.floor(Math.random() * (max - min + 1) + min)
// console.log(num2)
// 向下取整:Math.floor()
// 向上取整:Math.ceil()
// 四舍五入:Math.round()
生成一个指定范围、指定长度的随机数组
let min = 4
let max = 5
let arrLength = 5
let arr = []
for(let i = 0 ; i < arrLength ; i++){
let num1 = Math.floor(Math.random() * (max - min + 1) + min)
arr.push(num1)
}
console.log(arr)