using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Ant.Service.Utility { public class RandHelper { /// /// 生成随机数字 /// /// 生成长度 /// public static string Number(int Length) { return DateTimeUtility.DateTimeToStamp(DateTime.Now) + Number(Length, true); } /// /// 生成随机数字 /// /// 生成长度 /// 是否要在生成前将当前线程阻止以避免重复 /// public static string Number(int Length, bool Sleep) { if (Sleep) System.Threading.Thread.Sleep(100); string result = ""; System.Random random = new Random(); for (int i = 0; i < Length; i++) { result += random.Next(10).ToString(); } return result; } /// /// 生成随机字母与数字 /// /// 生成长度 /// public static string Str(int Length) { return Str(Length, true); } /// /// 生成随机字母与数字 /// /// 生成长度 /// 是否要在生成前将当前线程阻止以避免重复 /// public static string Str(int Length, bool Sleep) { if (Sleep) System.Threading.Thread.Sleep(100); 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' }; string result = ""; int n = Pattern.Length; System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks)); for (int i = 0; i < Length; i++) { int rnd = random.Next(0, n); result += Pattern[rnd]; } return result; } /// /// 生成随机字母 /// /// 生成长度 /// public static string Str_char(int Length) { return Str_char(Length, true); } /// /// 生成随机字母 /// /// 生成长度 /// 是否要在生成前将当前线程阻止以避免重复 /// public static string Str_char(int Length, bool Sleep) { if (Sleep) System.Threading.Thread.Sleep(100); char[] Pattern = new char[] { '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' }; string result = ""; int n = Pattern.Length; System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks)); for (int i = 0; i < Length; i++) { int rnd = random.Next(0, n); result += Pattern[rnd]; } return result; } } }