ScriptCore: Implemented Pick-Method and ThreadSafeRandom

This commit is contained in:
Simon Lübeß
2024-03-22 10:14:38 +01:00
parent f8b6c50cda
commit c6c47d0c0e
2 changed files with 48 additions and 0 deletions
+38
View File
@@ -0,0 +1,38 @@
using System;
namespace GlitchyEngine.Math;
/// <summary>
/// A thread safe global random number generator.
/// </summary>
/// <remarks>
/// Based on https://andrewlock.net/building-a-thread-safe-random-implementation-for-dotnet-framework/
/// </remarks>
public static class ThreadSafeRandom
{
[ThreadStatic]
private static Random? _local;
private static readonly Random Global = new();
/// <summary>
/// Gets a thread safe random number generator.
/// </summary>
public static Random Instance
{
get
{
if (_local is null)
{
int seed;
lock (Global)
{
seed = Global.Next();
}
_local = new Random(seed);
}
return _local;
}
}
}