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

原文地址:https://www.cnblogs.com/exyz/p/11971808.html