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的整數。
新聞熱點
疑難解答
圖片精選