// MIT License - Copyright (c) 2023 wallstop // Full license text: https://github.com/wallstop/unity-helpers/blob/main/LICENSE namespace WallstopStudios.UnityHelpers.Core.Random { #if !SINGLE_THREADED using System.Threading; #endif /// /// Provides a per-thread singleton instance for a given implementation. /// /// /// /// Many PRNGs are stateful and not thread-safe to share. This utility gives each thread its own instance, /// removing locking and state contention. Prefer accessing PRNGs via ThreadLocalRandom<T>.Instance /// or (which already uses a thread-local default) in multithreaded systems. /// /// public static class ThreadLocalRandom where T : IRandom, new() { #if SINGLE_THREADED public static readonly T Instance = new(); #else private static readonly ThreadLocal RandomCache = new(() => new T()); public static T Instance => RandomCache.Value; #endif } }