js随机数, 范围随机数 飞鸟和蝉

生成[0,n]范围内的随机整数

const n = 10
let res = Math.floor(Math.random()*n)
console.log(res)

生成[min,max]范围内的随机整数

const min = 90, max = 100;
let res = Math.floor(Math.random()* (max-min+1))+min
console.log(res)

生成[1,n]范围内随机数

const n = 10;
let res = Math.floor(Math.random()*n)+1
console.log(res)
原文地址:https://www.cnblogs.com/cl1998/p/15571919.html