js 获取指定范围随机数

一、随机获取1-10以内数字

let random = Math.floor((Math.random() * 10) + 1)

console.log(random)  // 6

二、随机获取数组内的值

let arr = ['a', 'b', 'c']
let index = Math.floor(Math.random() * arr.length)

console.log(arr[index])  // c

三、随机指定范围内值

function rand(m, n)  {
    return Math.ceil(Math.random() * (n-m+1) + m-1)
}

rand(60, 100)  // 随机输出60-100之间的值


版权声明:本文为qq_34402069原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。