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)
posted @ 2021-11-18 13:25  飞鸟和蝉-  阅读(125)  评论(0编辑  收藏  举报