123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- 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);
- }
-
-
-
-
-
-
- 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>(T[] array)
- {
- return array[RandomNumber(0, array.Length)];
- }
- public static T RandomArray<T>(List<T> array)
- {
- return array[RandomNumber(0, array.Count)];
- }
-
-
-
-
-
- public static void BreakRank<T>(List<T> 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;
- }
-
-
-
-
- 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();
- }
- }
-
-
-
-
-
-
- public static void Shuffle<T>(List<T> 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;
- }
- }
- }
- }
|