JS gets the random number function to customize the minimum maximum
- 2020-03-30 02:53:25
- OfStack
function selectFrom(lowerValue, upperValue){
//Total value range
var choices = upperValue - lowerValue + 1;
return Math.floor(Math.random() * choices + lowerValue);
}
var num = selectFrom(2, 10);
alert(num);//A value between 2 and 10 (including 2 and 10)
var colors = ["red","green","blue","yellow","black","brown"];
var color = colors[selectFrom(0, colors.length-1)];
alert(color);//It could be any value in the array