JS中產生20位隨機數以0-9為例也可以是a-z A-Z,0-9a-z

來源:互聯網
上載者:User

JS中產生20位隨機數以0-9為例也可以是a-z A-Z,0-9a-z

JS代碼:

function s20(){ var data=["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]; for(var j=0;j<500;j++){ //500為想要產生的行數var result="";for(var i=0;i<20;i++){ //產生20位就使i<20r=Math.floor(Math.random()*16); //16為數組裡面資料的數量,目的是以此當下標取數組data裡的值! result+=data[r]; //輸出20次隨機數的同時,讓rrr加20次,就是20位的隨機字串了。 } document.write(result); document.write("<br/>"); } }

完整html代碼:

<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <script type="text/javascript"> function s20(){ var data=["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]; for(var j=0;j<500;j++){ var result=""; for(var i=0;i<20;i++){ r=Math.floor(Math.random()*16);result+=data[r]; } document.write(result); document.write("<br/>"); } } </script> </head> <body> <input type="button" onclick="s20()" value="產生隨機數"> </body> </html>

問問題啦,怎產生0-9以及a-z的隨機數?我知道這個Cstr(Chr((57-48)*rnd+48))是產生0-9

Dim v as String
Dim tmp as Integer
Randomize
Do Until (tmp>47 And tmp<58) Or (tmp>64 And tmp<91) Or (tmp>96 And tmp<123)
tmp = Int((122 * Rnd))
Loop
v = chr(tmp)
MsgBox v
 
一 Regex 只允許a-z,A-Z,0-9與_還有-

js正則寫法

/[-_\da-z]/ig

要是回答的內容有問題,或認為不妥,請發送百度訊息給我,訊息內容加上本頁網址哦。。

·
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.