RandomHelper.cs 775 B

123456789101112131415161718192021222324252627282930313233343536
  1. using System;
  2. namespace Base
  3. {
  4. public static class RandomHelper
  5. {
  6. public static UInt64 RandUInt64()
  7. {
  8. var bytes = new byte[8];
  9. Random random = new Random();
  10. random.NextBytes(bytes);
  11. return BitConverter.ToUInt64(bytes, 0);
  12. }
  13. public static Int64 RandInt64()
  14. {
  15. var bytes = new byte[8];
  16. Random random = new Random();
  17. random.NextBytes(bytes);
  18. return BitConverter.ToInt64(bytes, 0);
  19. }
  20. /// <summary>
  21. /// 获取lower与Upper之间的随机数
  22. /// </summary>
  23. /// <param name="lower"></param>
  24. /// <param name="upper"></param>
  25. /// <returns></returns>
  26. public static int RandomNumber(int lower, int upper)
  27. {
  28. Random ra = new Random(Guid.NewGuid().GetHashCode());
  29. int value = ra.Next(lower, upper);
  30. return value;
  31. }
  32. }
  33. }