RandomHelper.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace IoTIntegrationPlatform.Common
  7. {
  8. /// <summary>
  9. /// 随机数
  10. /// </summary>
  11. public static class RandomHelper
  12. {
  13. /// <summary>
  14. /// 生成指定长度的随机数
  15. /// </summary>
  16. /// <param name="length"></param>
  17. /// <returns></returns>
  18. public static string GenerateRandomStr(int length)
  19. {
  20. StringBuilder sb = new StringBuilder();
  21. try
  22. {
  23. //const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  24. const string chars = "ABCDEF0123456789";
  25. Random rand = new Random();
  26. for (int i = 0; i < length; i++) // 生成指定长度的随机字符串
  27. {
  28. sb.Append(chars[rand.Next(chars.Length)]);
  29. }
  30. }
  31. catch (Exception ex)
  32. {
  33. Logging.Error(ex, ex.Message);
  34. }
  35. return sb.ToString();
  36. }
  37. }
  38. }