sharp-chat/SharpChat/RNG.cs

31 lines
837 B
C#
Raw Normal View History

2022-08-30 15:00:58 +00:00
using System;
using System.Security.Cryptography;
namespace SharpChat {
public static class RNG {
2023-02-07 15:01:56 +00:00
private static object Lock { get; } = new();
private static Random NormalRandom { get; } = new();
2022-08-30 15:00:58 +00:00
private static RandomNumberGenerator SecureRandom { get; } = RandomNumberGenerator.Create();
public static int Next() {
2023-02-07 15:01:56 +00:00
lock(Lock)
2022-08-30 15:00:58 +00:00
return NormalRandom.Next();
}
public static int Next(int max) {
2023-02-07 15:01:56 +00:00
lock(Lock)
2022-08-30 15:00:58 +00:00
return NormalRandom.Next(max);
}
public static int Next(int min, int max) {
2023-02-07 15:01:56 +00:00
lock(Lock)
2022-08-30 15:00:58 +00:00
return NormalRandom.Next(min, max);
}
public static void NextBytes(byte[] buffer) {
lock(Lock)
SecureRandom.GetBytes(buffer);
}
}
}