JavaScript 在两个数字之间随机

示例

返回介于min和之间的随机整数max:

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

例子:

// randomBetween(0,10);
Math.floor(Math.random() * 11);

// randomBetween(1,10);
Math.floor(Math.random() * 10) + 1;

// randomBetween(5,20);
Math.floor(Math.random() * 16) + 5;

// randomBetween(-10,-2);
Math.floor(Math.random() * 9) - 10;