美文网首页
Math对象

Math对象

作者: 楼水流云 | 来源:发表于2019-10-08 15:09 被阅读0次

    是js的内置对象,提供一系列数学常数和数学方法,Math只提供了静态的属性和方法,所以使用时不用实例化

    Math.random()//重要 获得一个随机数
    获得一个从1到10的随机数
    1 + Math.floor(Math.random() * 10)

    获得一个随机数组
    function random(a,b){
    return a + Math.floor(Math.random() * (b-a))
    }
    var arr = []
    for(var i = 0; i < 50;i++){
    arr.push(random(0,10))
    }
    console.log(arr)

    获得一个随机字符串
    function randomStr(len){
    var dict = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    var str = ''
    for(var i = 0 ; i < len; i++){
    str += dict[random(0,62)]
    }
    return str
    }
    randomStr(8) 一个8位长度的随机字符串

    获得一个随机的ip地址
    function randomIP(len){
    var arr = []
    for(i = 0; i < 4; i++){
    arr.push(random(0,256))
    }
    return arr.join('.')
    }

    Math .round(0.1) 等于0 Math.round(0.5) 等于1 用于四舍五入
    .abs(1)//1 Math.abs(-1) //1 abs方法返回参数值的绝对值
    .max(2,-1,5)//5 求这里面参数的最大的那个值
    .min(2,-1,5)//-1 求这里面参数的最小的那个值
    .floor(3.999)//3 整数 天花板 往下降
    .ceil(3.2)//4 整数 地板 往上加
    .pow(2,2)//4 几的几次方
    .sin(0)//0 三角函数 图形计算
    .cos(0)//1 三角函数 图形计算
    .tan(0)//0 三角函数 图形计算
    .PI
    .E
    .LN2
    .LN10
    .LOG2E
    .LOG10E

    相关文章

      网友评论

          本文标题:Math对象

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