可能重复: 在Javascript中生成特定范围内的随机数?
我想要生成一个50到100的随机数。
Say I wanted to generate a random number from 50 to 100.
我知道有:
Math.random()但是我只能找到从1到'x'的方法
but I could only find ways to go from 1 to 'x'
推荐答案来自 MDN on Math.random() :
// 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; }