Re: ASP.NET for C# 如何產生英數夾雜的密碼?

看板Web_Design作者 (傳說中的果汁學長)時間19年前 (2006/04/06 10:52), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串3/3 (看更多)
※ 引述《dinos (來游泳吧~~)》之銘言: : ※ 引述《jimshih (傳說中的果汁學長)》之銘言: : : 事情是這樣的 : : 我想在User建立資料時, 自動產生一組亂數密碼(英數夾雜) : : 可是我不知要如何產生英數夾雜的亂數密碼? (假設要8個字元) : : 嗯....只好上來請教一下高手了... : char* setRandomKey(int n) { : int i,j; : char s[n]; : randomize(); : for(i=0;i<n;i++) { : j=rand()%62; : if(j<10) { : s[i]=(char)(j+48); : } : else if(j<36) { : s[i]=(char)(j+55); : } : else if(j<62) { : s[i]=(char)(j+61); : } : } : return s; : } : 差不多是這樣吧 好久沒碰 C 了 XD 大推~! 這個函數蠻好用的 不過你用的是C寫的, 所以我改寫了一下, 讓它可以在 C# 下執行: private string setRandomKey(int n) { int j; char [] s = new char[n]; string result = ""; Random rdm1 ; for (int i=0;i<n;i++) { rdm1 = new Random(unchecked((int)DateTime.Now.Ticks)+i); j = rdm1.Next() % 62; if(j<10) { s[i] = (char)(j+48); } else if(j<36) { s[i] = (char)(j+55); } else if(j<62) { s[i] = (char)(j+61); } result += s[i].ToString(); } return result; } 注意, rdm1後面有多做 +i 這個動作 不然跑出來的字元會完全一樣 (現在cpu太快了呀~) -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.229.61.172
文章代碼(AID): #14D89cdl (Web_Design)
文章代碼(AID): #14D89cdl (Web_Design)