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


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的整数。

« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3