using System; using System.Collections.Generic; using Random = System.Random; using System.Security.Cryptography; namespace ET { // 支持多线程 public static class RandomGenerator { [StaticField] [ThreadStatic] private static Random random; private static Random GetRandom() { return random ??= new Random(Guid.NewGuid().GetHashCode()); } public static ulong RandUInt64() { int r1 = RandInt32(); int r2 = RandInt32(); return ((ulong)r1 << 32) & (ulong)r2; } public static int RandInt32() { return GetRandom().Next(); } public static uint RandUInt32() { return (uint) GetRandom().Next(); } public static long RandInt64() { uint r1 = RandUInt32(); uint r2 = RandUInt32(); return (long)(((ulong)r1 << 32) | r2); } /// /// 获取lower与Upper之间的随机数,包含下限,不包含上限 /// /// /// /// public static int RandomNumber(int lower, int upper) { int value = GetRandom().Next(lower, upper); return value; } public static bool RandomBool() { return GetRandom().Next(2) == 0; } public static T RandomArray(T[] array) { return array[RandomNumber(0, array.Length)]; } public static T RandomArray(List array) { return array[RandomNumber(0, array.Count)]; } /// /// 打乱数组 /// /// /// 要打乱的数组 public static void BreakRank(List arr) { if (arr == null || arr.Count < 2) { return; } for (int i = 0; i < arr.Count; i++) { int index = GetRandom().Next(0, arr.Count); (arr[index], arr[i]) = (arr[i], arr[index]); } } public static float RandFloat01() { int a = RandomNumber(0, 1000000); return a / 1000000f; } /// /// 随机一个6位数房间id /// /// public static string RandRoomId() { byte[] randomNumber = new byte[4]; using (RandomNumberGenerator rng = RandomNumberGenerator.Create()) { rng.GetBytes(randomNumber); uint value = BitConverter.ToUInt32(randomNumber, 0) % 900000 + 100000; // 确保是六位数 return value.ToString(); } } /// /// 乱序一个list /// /// /// public static void Shuffle(List list) { Random rng = new Random(); int n = list.Count; while (n > 1) { n--; int k = rng.Next(n + 1); T value = list[k]; list[k] = list[n]; list[n] = value; } } } }