using System;
namespace GlitchyEngine.Math;
///
/// A thread safe global random number generator.
///
///
/// Based on https://andrewlock.net/building-a-thread-safe-random-implementation-for-dotnet-framework/
///
public static class ThreadSafeRandom
{
[ThreadStatic]
private static Random? _local;
private static readonly Random Global = new();
///
/// Gets a thread safe random number generator.
///
public static Random Instance
{
get
{
if (_local is null)
{
int seed;
lock (Global)
{
seed = Global.Next();
}
_local = new Random(seed);
}
return _local;
}
}
}