欢迎您访问新疆栾骏商贸有限公司,公司主营电子五金轴承产品批发业务!
全国咨询热线: 400-8878-609

新闻资讯

常见问题

js生成随机数之random函数随机示例

作者:用户投稿2026-01-11 15:57:37
JavaScript Math.random()内置函数
random函数返回值
返回0和1之间的伪随机数,可能为0,但总是小于1,[0,1)
random函数示例
//返回随机数
document.write(Math.random());
//返回10-20的随机数
document.write(Math.random()*(20-10)+10);
//返回指定范围的随机数(m-n之间)的公式
document.write(Math.random()*(n-m)+m);
基于时间,亦可以产生随机数
复制代码 代码如下:
var now=new Date();
var number = now.getSeconds(); //这将产生一个基于目前时间的0到59的整数。
var now=new Date();
var number = now.getSeconds()%43; //这将产生一个基于目前时间的0到42的整数。