RandHelper.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Ant.Service.Utility
  7. {
  8. public class RandHelper
  9. {
  10. /// <summary>
  11. /// 生成随机数字
  12. /// </summary>
  13. /// <param name="length">生成长度</param>
  14. /// <returns></returns>
  15. public static string Number(int Length)
  16. {
  17. return DateTimeUtility.DateTimeToStamp(DateTime.Now) + Number(Length, true);
  18. }
  19. /// <summary>
  20. /// 生成随机数字
  21. /// </summary>
  22. /// <param name="Length">生成长度</param>
  23. /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
  24. /// <returns></returns>
  25. public static string Number(int Length, bool Sleep)
  26. {
  27. if (Sleep)
  28. System.Threading.Thread.Sleep(100);
  29. string result = "";
  30. System.Random random = new Random();
  31. for (int i = 0; i < Length; i++)
  32. {
  33. result += random.Next(10).ToString();
  34. }
  35. return result;
  36. }
  37. /// <summary>
  38. /// 生成随机字母与数字
  39. /// </summary>
  40. /// <param name="IntStr">生成长度</param>
  41. /// <returns></returns>
  42. public static string Str(int Length)
  43. {
  44. return Str(Length, true);
  45. }
  46. /// <summary>
  47. /// 生成随机字母与数字
  48. /// </summary>
  49. /// <param name="Length">生成长度</param>
  50. /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
  51. /// <returns></returns>
  52. public static string Str(int Length, bool Sleep)
  53. {
  54. if (Sleep)
  55. System.Threading.Thread.Sleep(100);
  56. 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' };
  57. string result = "";
  58. int n = Pattern.Length;
  59. System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
  60. for (int i = 0; i < Length; i++)
  61. {
  62. int rnd = random.Next(0, n);
  63. result += Pattern[rnd];
  64. }
  65. return result;
  66. }
  67. /// <summary>
  68. /// 生成随机字母
  69. /// </summary>
  70. /// <param name="IntStr">生成长度</param>
  71. /// <returns></returns>
  72. public static string Str_char(int Length)
  73. {
  74. return Str_char(Length, true);
  75. }
  76. /// <summary>
  77. /// 生成随机字母
  78. /// </summary>
  79. /// <param name="Length">生成长度</param>
  80. /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
  81. /// <returns></returns>
  82. public static string Str_char(int Length, bool Sleep)
  83. {
  84. if (Sleep)
  85. System.Threading.Thread.Sleep(100);
  86. 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' };
  87. string result = "";
  88. int n = Pattern.Length;
  89. System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
  90. for (int i = 0; i < Length; i++)
  91. {
  92. int rnd = random.Next(0, n);
  93. result += Pattern[rnd];
  94. }
  95. return result;
  96. }
  97. }
  98. }