美文网首页
Math数学函数

Math数学函数

作者: 可爱的木头 | 来源:发表于2017-05-03 17:30 被阅读0次
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));
}

相关文章

  • Python学习笔记(2014.10.02)

    math模块(数学函数): 使用math模块 import mathdir(math) 这句可查...

  • Golang标准库——math

    math math包提供了基本的数学常数和数学函数。 Constants 数学常数,参见:http://oeis....

  • c/c++常用标准头文件函数总结

    一、常用函数 1、数学函数头文件 #include 或者 #include ...

  • Math

    Math是一个内置对象,它拥有一些数学常数属性和数学函数方法。Math 不是一个函数对象。Math 用于 Numb...

  • 数学Math函数

    1. Math.random(): 结果为0-1间的一个随机数(包括0,不包括1)。 0<=Math.rando...

  • Math数学函数

  • 数学函数 math

    函数名函数内容math.ceil(x)向上取整,返回 x 的上限,即大于或者等于 x 的最小整数math.floo...

  • Java中的Math对象

    Math Math是一个内置对象, 它具有数学常数和函数的属性和方法。不是一个函数对象。 Math的属性 Math...

  • Date Math

    6.5 date math对象的学习 Math对象 Math对象:仅专门提供数学计算的方法Math对象没有构造函数...

  • Math&Date

    Math 数学函数:它属于对象数据类型 typeof Math ->'object'Math对象中提供了很多操作...

网友评论

      本文标题:Math数学函数

      本文链接:https://www.haomeiwen.com/subject/bacltxtx.html