123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- using System;
- using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
- using System.Threading;
- namespace TEAMModelBI.Tool.Extension
- {
- public static class GenerateRandom
- {
- static char[] ch_Number = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
- static char[] ch_Upper = 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' };
- static char[] ch_Lower = 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' };
- /// <summary>
- /// 生成随机数字
- /// </summary>
- /// <param name="Length">生成长度</param>
- /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
- /// <returns></returns>
- public static string Number(int Length, bool Sleep = false)
- {
- if (Sleep) Thread.Sleep(3);
- string result = "";
- Random random = new Random();
- for (int i = 0; i < Length; i++)
- {
- result += random.Next(10).ToString();
- }
- return result;
- }
- /// <summary>
- /// 生成随机大写字母与数字
- /// </summary>
- /// <param name="Length">生成长度</param>
- /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
- /// <returns></returns>
- public static string StrNumberUpeper(int Length, bool Sleep = false)
- {
- if (Sleep) Thread.Sleep(3);
- var numUpper = ch_Number.Concat(ch_Upper).ToArray();
- StringBuilder result = new();
- int l = numUpper.Length;
- Random random = new(~unchecked((int)DateTime.Now.Ticks));
- for (int i = 0; i < Length; i++)
- {
- result.Append(numUpper[random.Next(0, l)]);
- }
- return result.ToString();
- }
- /// <summary>
- /// 生成随机小写字母
- /// </summary>
- /// <param name="Length">生成长度</param>
- /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
- /// <returns></returns>
- public static string StrLower(int length, bool sleep = false)
- {
- if (sleep) Thread.Sleep(3);
- StringBuilder result = new();
- int l = ch_Lower.Length;
- Random random = new(~unchecked((int)DateTime.Now.Ticks));
- for (int i = 0; i < length; i++)
- {
- result.Append(ch_Lower[random.Next(0, l)]);
- }
- return result.ToString();
- }
- /// <summary>
- /// 随机生产大小写字母和数字的随机数
- /// </summary>
- /// <param name="length">生成长度</param>
- /// <param name="sleep">是否要在生成前将当前线程阻止以避免重复</param>
- /// <returns></returns>
- public static string StrAll(int length, bool sleep = false)
- {
- if (sleep) Thread.Sleep(3);
- StringBuilder result = new();
- var temp = ch_Number.Concat(ch_Upper).ToArray();
- var allchar = ch_Number.Concat(temp).ToArray();
- int l = allchar.Length;
- Random random = new(~unchecked((int)DateTime.Now.Ticks));
- for (int i = 0; i < length; i++)
- {
- result.Append(allchar[random.Next(0, l)]);
- }
- return result.ToString();
- }
- /// <summary>
- /// 生成随机数
- /// </summary>
- /// <param name="length">生成长度</param>
- /// <param name="sleep">是否要在生成前将当前线程阻止以避免重复</param>
- /// <param name="large">是否需要大写字母</param>
- /// <param name="small">是否需要小写字母</param>
- /// <returns></returns>
- public static string StrRandom(int length, bool sleep = false, bool large = false, bool small = false)
- {
- if (sleep) Thread.Sleep(3);
- char[] ch = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
- if (large == true) ch = ch.Concat(ch_Upper).ToArray();
- if (small == true) ch = ch.Concat(ch_Lower).ToArray();
- StringBuilder result = new();
- int l = ch.Length;
- Random random = new(~unchecked((int)DateTime.Now.Ticks));
- for (int i = 0; i < length; i++)
- {
- result.Append(ch[random.Next(0, l)]);
- }
- return result.ToString();
- }
- }
- }
|