js中的Math对象

1.使用Math对象的min()和max方法获取数组最大值或最小值

2.使用Math对象的舍入方法:

Math.ceil():向上舍入

Math.floor():向下舍入

Math.round():标准四舍五入

3.生成随机数:

公式:值=Math.floor(Math.random()*可能值得总数 + 第一个可能的值)

<script type="text/javascript">
    //生成一个1至10的整数
        // var num=Math.floor(Math.random()*10+1);
        // console.log(num)
    //对上面进行封装一下,范围可自己选择 lowerValue ---- upperValue
        function randomNum(lowerValue,upperValue){
            return Math.floor(Math.random()*(upperValue-lowerValue+1)+lowerValue);
        }
        console.log(randomNum(1,100));

</script>

测试结果(刷新可看变化):

 

转载于:https://www.cnblogs.com/xingguozhiming/p/8636036.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值