function ran(m) { m = m > 13 ? 13 : m; var num = new Date().getTime(); return num.toString().substring(13 - m); } console.log(ran(5));
根据Math的random函数生成的随机数截取m位,生成的随机数最大不超过16位,能保证首位不为0
function rand(m) { m = m > 16 ? 16 : m; var num = Math.random().toString(); if(num.substr(num.length - m, 1) === '0') { return rand(m); } return num.substring(num.length - m); } console.log(rand(5));
根据Math的random函数生成,位数没有限制,首位不为0
function rando(m) { var num = ''; for(var i = 0; i < m; i++) { var val = parseInt(Math.random()*10, 10); if(i === 0 && val === 0) { i--; continue; } num += val; } return num; } console.log(rando(5));
您可能感兴趣的文章: