javascript根據時間產生m位隨機數最大13位,隨機數13位

來源:互聯網
上載者:User

javascript根據時間產生m位隨機數最大13位,隨機數13位

根據時間產生m位隨機數,最大13位隨機數,並且不能保證首位不為0

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));

用Javascript編程產生10個以內的隨機數

<html>
<head>
<title> 產生隨機數 </title>
</head>
<body>
<script type="text/javascript">

for(i = 1; i < 11; i++) {
//Math.random()是產生0~1之間的隨機數
var ran = Math.random()*10; //產生0~10之間的隨機數
document.write("第" + i + "個隨機數:" + ran + "<br> ");

}

</script>
</body>
</html>
 
用javascript做一個產生0-10的隨機數

document.write(parseInt(10*Math.random()));  //輸出0~10之間的隨機整數
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.