sofii/SoFii/RNG.cs

44 lines
1.1 KiB
C#
Raw Normal View History

2023-10-08 00:10:21 +00:00
using System;
namespace SoFii {
public static class RNG {
private static object Lock { get; } = new object();
private static Random NormalRandom { get; } = new Random();
public static int Next() {
lock(Lock)
return NormalRandom.Next();
}
public static int Next(int max) {
lock(Lock)
return NormalRandom.Next(max);
}
public static int Next(int min, int max) {
lock(Lock)
return NormalRandom.Next(min, max);
}
public static void NextBytes(byte[] buffer) {
lock(Lock)
NormalRandom.NextBytes(buffer);
}
public static void Shuffle<T>(T[] arr) {
if(arr is null)
throw new ArgumentNullException(nameof(arr));
lock(Lock) {
int n = arr.Length;
while(n > 1) {
int k = Next(n--);
T tmp = arr[n];
arr[n] = arr[k];
arr[k] = tmp;
}
}
}
}
}