假设我想生成一个从 50 到 100 的随机数。
我知道有:
Math.random()
但我只能找到从 1 到“x”的方法
// Returns a random integer between min and max
// Using Math.round() will give you a non-uniform distribution!
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
I believe this would also work:
function randomInt(min,range) {
return Math.floor((Math.random()*(range+1))+min)
}
In your case min would be 50 and range would be 50