日期:2014-05-16 浏览次数:20332 次
var num = Math.floor(Math.random() * 10 + 1); //产生1到10的值 var num2 = Math.floor(Math.random() * 9 + 2); //产生2到10的值随机选择函数:
function selectFrom(lowerValue, upperValue) { var choices = upperValue - lowerValue + 1; return Math.floor(Math.random() * choices + lowerValue); } var num = selectFrom(2, 10); alert(num); //介于2至10之间 var colors = ["red", "green", "blue", "yellow", "black", "purple", "brown"]; var color = colors[selectFrom(0, colors.length-1)]; alert(color); //数组中可能的任何一项