GenerateRandom.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Linq;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. using System.Threading;
  6. namespace TEAMModelBI.Tool.Extension
  7. {
  8. public static class GenerateRandom
  9. {
  10. static char[] ch_Number = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
  11. 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' };
  12. 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' };
  13. /// <summary>
  14. /// 生成随机数字
  15. /// </summary>
  16. /// <param name="Length">生成长度</param>
  17. /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
  18. /// <returns></returns>
  19. public static string Number(int Length, bool Sleep = false)
  20. {
  21. if (Sleep) Thread.Sleep(3);
  22. string result = "";
  23. Random random = new Random();
  24. for (int i = 0; i < Length; i++)
  25. {
  26. result += random.Next(10).ToString();
  27. }
  28. return result;
  29. }
  30. /// <summary>
  31. /// 生成随机大写字母与数字
  32. /// </summary>
  33. /// <param name="Length">生成长度</param>
  34. /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
  35. /// <returns></returns>
  36. public static string StrNumberUpeper(int Length, bool Sleep = false)
  37. {
  38. if (Sleep) Thread.Sleep(3);
  39. var numUpper = ch_Number.Concat(ch_Upper).ToArray();
  40. StringBuilder result = new();
  41. int l = numUpper.Length;
  42. Random random = new(~unchecked((int)DateTime.Now.Ticks));
  43. for (int i = 0; i < Length; i++)
  44. {
  45. result.Append(numUpper[random.Next(0, l)]);
  46. }
  47. return result.ToString();
  48. }
  49. /// <summary>
  50. /// 生成随机小写字母
  51. /// </summary>
  52. /// <param name="Length">生成长度</param>
  53. /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
  54. /// <returns></returns>
  55. public static string StrLower(int length, bool sleep = false)
  56. {
  57. if (sleep) Thread.Sleep(3);
  58. StringBuilder result = new();
  59. int l = ch_Lower.Length;
  60. Random random = new(~unchecked((int)DateTime.Now.Ticks));
  61. for (int i = 0; i < length; i++)
  62. {
  63. result.Append(ch_Lower[random.Next(0, l)]);
  64. }
  65. return result.ToString();
  66. }
  67. /// <summary>
  68. /// 随机生产大小写字母和数字的随机数
  69. /// </summary>
  70. /// <param name="length">生成长度</param>
  71. /// <param name="sleep">是否要在生成前将当前线程阻止以避免重复</param>
  72. /// <returns></returns>
  73. public static string StrAll(int length, bool sleep = false)
  74. {
  75. if (sleep) Thread.Sleep(3);
  76. StringBuilder result = new();
  77. var temp = ch_Number.Concat(ch_Upper).ToArray();
  78. var allchar = ch_Number.Concat(temp).ToArray();
  79. int l = allchar.Length;
  80. Random random = new(~unchecked((int)DateTime.Now.Ticks));
  81. for (int i = 0; i < length; i++)
  82. {
  83. result.Append(allchar[random.Next(0, l)]);
  84. }
  85. return result.ToString();
  86. }
  87. /// <summary>
  88. /// 生成随机数
  89. /// </summary>
  90. /// <param name="length">生成长度</param>
  91. /// <param name="sleep">是否要在生成前将当前线程阻止以避免重复</param>
  92. /// <param name="large">是否需要大写字母</param>
  93. /// <param name="small">是否需要小写字母</param>
  94. /// <returns></returns>
  95. public static string StrRandom(int length, bool sleep = false, bool large = false, bool small = false)
  96. {
  97. if (sleep) Thread.Sleep(3);
  98. char[] ch = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
  99. if (large == true) ch = ch.Concat(ch_Upper).ToArray();
  100. if (small == true) ch = ch.Concat(ch_Lower).ToArray();
  101. StringBuilder result = new();
  102. int l = ch.Length;
  103. Random random = new(~unchecked((int)DateTime.Now.Ticks));
  104. for (int i = 0; i < length; i++)
  105. {
  106. result.Append(ch[random.Next(0, l)]);
  107. }
  108. return result.ToString();
  109. }
  110. }
  111. }