随机函数random()

在这里插入图片描述

菜鸟教程解释,JavaScript random()方法,返回介于0(包含)~1(不包含)之间的一个随机数

大家可能会有点搞混淆迷糊,大家直接记住这句话就行了:Javascript random()方法 ,返回0< x <1之间的随机小数

请添加图片描述

	<script>
        console.log(Math.ceil(1.3)); // 结果为2 ,向上取整
        console.log(Math.floor(1.5)); // 结果为1 ,向下取整
        console.log(Math.round(1.5)); // 结果为2 ,四舍五入

        console.log(Math.random()); // 0< x <1 之间的随机小数
        console.log(Math.random() * 10); // 0< x <10 的随机小数

        console.log(Math.round(Math.random())); // 随机取0或1的整数
        console.log(Math.round(Math.random() * 10)); // 0<= x <=10 之间的随机整数
        console.log(Math.floor(Math.random() * 10)); // 0<= x <=9 之间的随机整数
        console.log(Math.ceil(Math.random() * 10)); // 1<= x <=10 之间的随机整数
        console.log(parseInt(Math.random() * 10 + 1)); // 1<= x <=10 之间的随机整数 

       /*  parseInt(Math.random() * (m - n) + n) // 生成 [n,m),包含n但不包含m的正整数:
        parseInt(Math.random() * (m - n) + n) + 1 // 生成 (n,m],不包含n但包含m的正整数:
        parseInt(Math.random() * (m - n + 1) + n) // 生成 [n,m],包含n和m的随机数:
        parseInt(Math.random() * (m - n - 1) + n + 1) // 生成 (n,m),不包含n和m的正整数: */
    </script>

相关资料 https://blog.csdn.net/wenfu814/article/details/108455210?spm=1001.2014.3001.5506

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

*neverGiveUp*

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值