js生成随机数

1.

/*----------------------------------js生成随机四位数用户删除---------------------*/
function mathRand() 
{ 
    var num=""; 
    for(var i=0;i<4;i++) 
    { 
        num+=Math.floor(Math.random()*10); 
    } 
    return num;
} 

 

 

2.Math.floor() 方法

 floor() 方法可对一个数进行下舍入。

  例如:

      

<script type="text/javascript">

document.write(Math.floor(0.60) + "<br />")
document.write(Math.floor(0.40) + "<br />")
document.write(Math.floor(5) + "<br />")
document.write(Math.floor(5.1) + "<br />")
document.write(Math.floor(-5.1) + "<br />")
document.write(Math.floor(-5.9))

</script>
输出:
0
0
5
5
-6
-6

 

3.Math.random()方法

    ----   random() 方法可返回介于 0 ~ 1 之间的一个随机数。

在本例中,我们将取得介于 0 到 1 之间的一个随机数:
<script type="text/javascript">

document.write(Math.random())

</script>
输出:
0.6627135668146795

 

posted @ 2017-01-18 15:01  Mr_伍先生  阅读(993)  评论(0编辑  收藏  举报