我该如何生成7到10之间的数字?目前我只能生成0到10之间的数字:
Math.floor(Math.random()*11)
我该如何生成7到10之间的数字?目前我只能生成0到10之间的数字:
Math.floor(Math.random()*11)
function getRandom(min, max) {
return min + Math.floor(Math.random() * (max - min + 1));
}
for(var x = 0; x < 5; x++) {
console.log(getRandom(7, 10));
}
Math.floor(7 + Math.random() * 4)
将生成从7到10(包括7和10)的数字。
只需这样说:
Math.floor(Math.random()*4) + 7
7 + Math.floor(Math.random()*4)