生成不从零开始的范围内的随机整数

Win*_*ung 19 javascript random

如何生成7到10之间的数字?到目前为止,我所知道的是在0-10的范围内产生:

Math.floor(Math.random()*11)
Run Code Online (Sandbox Code Playgroud)

Jor*_*dan 62

function getRandom(min, max) {
    return min + Math.floor(Math.random() * (max - min + 1));
}

for(var x = 0; x < 5; x++) {
    alert(getRandom(7, 10));
}
Run Code Online (Sandbox Code Playgroud)

  • +1使其成为基于min,max的通用函数. (8认同)

Dav*_*nco 21

Math.floor(7 + Math.random() * 4) 将生成7到10之间的数字.