Generate random number, between 2 numbers? [duplic

2019-04-12 22:38发布

Possible Duplicate:
Generating random numbers in Javascript in a specific range?

Say I wanted to generate a random number from 50 to 100.

I know there's:

Math.random()

but I could only find ways to go from 1 to 'x'

2条回答
贼婆χ
2楼-- · 2019-04-12 22:48

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

查看更多
霸刀☆藐视天下
3楼-- · 2019-04-12 23:14

From 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;
}
查看更多
登录 后发表回答