请选择 进入手机版 | 继续访问电脑版
开启辅助访问
帐号登录 |立即注册

JS获取两个数之间随机整数的方法

 
js获取0-1随机数,包含0,不好含1
var num = Math.random();
console.log(num);



1、获取1-10之间的随机数
var num = Math.floor(Math.random() * 10) + 1;
console.log(num);
//Math.floor((Math.random()*(10-1+1))+1
2.获取两个数之间的随机整数
function getRandomNumberByRange(start, end) {
    return Math.floor(Math.random() * (end - start+1) + start)
}
举例:获取9-2的随机数
//Math.floor((Math.random()*(9-2+1))+2
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则