Generate random number, between 2 numbers? [duplicate]

吃可爱长大的小学妹 提交于 2020-01-13 05:43:27

问题


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'


回答1:


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;
}



回答2:


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



来源:https://stackoverflow.com/questions/13997793/generate-random-number-between-2-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!