C# 生成隨機數字母加數字


 1         /// <summary>
 2         /// 生成隨機字母與數字
 3         /// </summary>
 4         /// <param name="Length">生成長度</param>
 5         /// <param name="Sleep">是否要在生成前將當前線程阻止以避免重復</param>
 6         /// <returns></returns>
 7         public static string StrRandom(int Length, bool Sleep)
 8         {
 9             if (Sleep) System.Threading.Thread.Sleep(3);
10             char[] Pattern = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
11             string result = "";
12             int n = Pattern.Length;
13             System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
14             for (int i = 0; i < Length; i++)
15             {
16                 int rnd = random.Next(0, n);
17                 result += Pattern[rnd];
18             }
19             return result;
20         }

簡要說明:字符串可自定義可添加標點符號或去除字母或數字、可增加時間戳或Guid


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM