有帮助?打赏吧!

随机生成6位数的 几种写法

方法1: 

var code=String(Math.random()).substr(2,6)
 
方法2:
 
function getCode() {
        var code = "";
        for (var i = 0; i < 6; i++) {
            code += parseInt(10 * Math.random())
        }
        return code;
    }

 

一般Math.random()会生成如下形式的数据。

0.28291609045118093
0.6186690246686339
0.0052408622577786446

如果你要生成更多位的随机数(超出了Math.random的小数位数),那么方法1,你可以再来一次String 相加即可。

 

 

 
posted @ 2015-04-22 15:01  luoeeyang  Views(1338)  Comments(0Edit  收藏  举报