1. 取介于 0(包含) ~ 1(不包含) 之间的一个随机数:
const number = Math.random();
console.log(number);//0.1692059011145015
2.取介于 1 到 10 之间的一个随机数:
const abs = Math.floor((Math.random() * 10) + 1);
console.log(abs); // 2
3.取介于 1 到 100 之间的一个随机数:
const number = Math.floor((Math.random() * 100) + 1);
console.log(number); // 29
4.取min(包含)~ max(包含)之间的数字:
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
let number = getRndInteger(2, 6);
console.log(number); // 3