Math数学函数

Math数学函数_第1张图片
QQ图片20170503172808.png
//Math.random
function selectFrom(lowerValue,upperValue){
   var choices = upperValue - lowerValue + 1;
   return Math.floor(Math.random() * choices + lowerValue);
}
var num = selectFrom(2,10)//介与2和10之间的一个整数
var colors = ["red","green","blue","yellow","black"];
var color = colors[selectFrom(0,colors.length-1)];
alert(color);//可能是数组中包含的任何一个字符串
//随机数取整
function randomInt(x1,x2)
{
  var min_int = parseInt(x1);
  var max_int = parseInt(x2);
  if ( isNaN(min_int) || isNaN(max_int) )
  {
    alert('parameter error');
    return false;
  }
  x1 = Math.min(min_int,max_int);
  x2 = Math.max(min_int,max_int);
 
  return x1 + Math.floor(Math.random() * (x2 - x1 + 1));
}

你可能感兴趣的:(Math数学函数)