您好,欢迎来到要发发知识网。
搜索
您的当前位置:首页js的math对象random()方法

js的math对象random()方法

来源:要发发知识网

JavaScript中Math是一个对象方法,方法中有很很多方便好用的数学方法。对于我们想要在网页上随机显示一些名人名言和新闻事件。可以使用js的math对象random()方法,它生成0-1的随机数。本文向大家介绍Math.random() 方法。

1、Math.random() 方法

生成0-1的随机数,包含0,不包含1,结果是浮点数。这个方法没有参数。

2、使用方法

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

3、返回值

返回一个介于 0 和 1 之间的随机数

4、使用实例

console.log(Math.random());
// 0.7069207248635578
 
console.log(Math.random());
// 0.765046694794209
 
console.log(Math.random());
// 0.14069121642698246

Math.random() 方法非常实用,对于想去随机数的小伙伴来说,这个方法要掌握哦~

Copyright © 2019- net188.cn 版权所有 湘ICP备2022005869号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务