mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 21:01:52 +00:00
ScriptCore: Implemented Pick-Method and ThreadSafeRandom
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user