RandomGenerator.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections.Generic;
  3. using Random = System.Random;
  4. using System.Security.Cryptography;
  5. namespace ET
  6. {
  7. // 支持多线程
  8. public static class RandomGenerator
  9. {
  10. [StaticField]
  11. [ThreadStatic]
  12. private static Random random;
  13. private static Random GetRandom()
  14. {
  15. return random ??= new Random(Guid.NewGuid().GetHashCode());
  16. }
  17. public static ulong RandUInt64()
  18. {
  19. int r1 = RandInt32();
  20. int r2 = RandInt32();
  21. return ((ulong)r1 << 32) & (ulong)r2;
  22. }
  23. public static int RandInt32()
  24. {
  25. return GetRandom().Next();
  26. }
  27. public static uint RandUInt32()
  28. {
  29. return (uint) GetRandom().Next();
  30. }
  31. public static long RandInt64()
  32. {
  33. uint r1 = RandUInt32();
  34. uint r2 = RandUInt32();
  35. return (long)(((ulong)r1 << 32) | r2);
  36. }
  37. /// <summary>
  38. /// 获取lower与Upper之间的随机数,包含下限,不包含上限
  39. /// </summary>
  40. /// <param name="lower"></param>
  41. /// <param name="upper"></param>
  42. /// <returns></returns>
  43. public static int RandomNumber(int lower, int upper)
  44. {
  45. int value = GetRandom().Next(lower, upper);
  46. return value;
  47. }
  48. public static bool RandomBool()
  49. {
  50. return GetRandom().Next(2) == 0;
  51. }
  52. public static T RandomArray<T>(T[] array)
  53. {
  54. return array[RandomNumber(0, array.Length)];
  55. }
  56. public static T RandomArray<T>(List<T> array)
  57. {
  58. return array[RandomNumber(0, array.Count)];
  59. }
  60. /// <summary>
  61. /// 打乱数组
  62. /// </summary>
  63. /// <typeparam name="T"></typeparam>
  64. /// <param name="arr">要打乱的数组</param>
  65. public static void BreakRank<T>(List<T> arr)
  66. {
  67. if (arr == null || arr.Count < 2)
  68. {
  69. return;
  70. }
  71. for (int i = 0; i < arr.Count; i++)
  72. {
  73. int index = GetRandom().Next(0, arr.Count);
  74. (arr[index], arr[i]) = (arr[i], arr[index]);
  75. }
  76. }
  77. public static float RandFloat01()
  78. {
  79. int a = RandomNumber(0, 1000000);
  80. return a / 1000000f;
  81. }
  82. /// <summary>
  83. /// 随机一个6位数房间id
  84. /// </summary>
  85. /// <returns></returns>
  86. public static string RandRoomId()
  87. {
  88. byte[] randomNumber = new byte[4];
  89. using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
  90. {
  91. rng.GetBytes(randomNumber);
  92. uint value = BitConverter.ToUInt32(randomNumber, 0) % 900000 + 100000; // 确保是六位数
  93. return value.ToString();
  94. }
  95. }
  96. /// <summary>
  97. /// 乱序一个list
  98. /// </summary>
  99. /// <param name="list"></param>
  100. /// <typeparam name="T"></typeparam>
  101. public static void Shuffle<T>(List<T> list)
  102. {
  103. Random rng = new Random();
  104. int n = list.Count;
  105. while (n > 1)
  106. {
  107. n--;
  108. int k = rng.Next(n + 1);
  109. T value = list[k];
  110. list[k] = list[n];
  111. list[n] = value;
  112. }
  113. }
  114. }
  115. }