只需按比例缩放结果:
function randomInRange(from, to) {
var r = Math.random();
return Math.floor(r * (to - from) + from);
}
function randomXToY(minVal,maxVal)
{
var randVal = minVal+(Math.random()*(maxVal-minVal));
return Math.round(randVal);
}
使用:
var random = randomXToY(101, 999);
Math.floor(Math.random()*898)+101