Math对象

Math对象不是构造函数,它具有数学常数和函数的属性和方法,都是以静态成员的方式提供跟数学相关的运算来找Math中的成员。

Math.PI                   // 圆周率
Math.random()           // 生成随机数
Math.floor()/Math.ceil()    // 向下取整/向上取整
Math.round()            // 取整,四舍五入
Math.abs()             // 绝对值
Math.max()/Math.min()      // 求最大和最小值
Math.sin()/Math.cos()      // 正弦/余弦
Math.power()/Math.sqrt()    // 求指数次幂/求平方根
  1. 求10-20之间的随机数
 // 生成 min 到 max 之间的随机数
    var min = 10;
    var max = 20;
    console.log( parseInt(Math.random() * (max - min) + min));
  1. 随机生成颜色RGB [0, 255] 整数
 function random(min, max) {
      return Math.floor(Math.random() * (max - min + 1) + min);
    }

    function randomRGB(min, max) {
      var color1 = random(min, max);
      var color2 = random(min, max);
      var color3 = random(min, max);

      return 'rgb(' + color1 + ', ' + color2 + ', ' + color3 + ')';
    }

    console.log(randomRGB(0, 255))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值