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
|
|