|
| 1 | +using SquidStd.Core.Interfaces.Rng; |
| 2 | +using SquidStd.Core.Types; |
| 3 | + |
| 4 | +namespace SquidStd.Core.Rng; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Entry point for obtaining <see cref="IRandom" /> instances. Consumers depend only on this factory |
| 8 | +/// and <see cref="IRandom" />, never on the underlying NRandom generators. |
| 9 | +/// </summary> |
| 10 | +public static class RandomFactory |
| 11 | +{ |
| 12 | + private const int ChaChaRounds = 20; |
| 13 | + |
| 14 | + /// <summary>A shared, thread-safe random source seeded non-deterministically.</summary> |
| 15 | + public static IRandom Shared { get; } = new NRandomAdapter(NRandom.RandomEx.Shared); |
| 16 | + |
| 17 | + /// <summary>Creates a random source using the default algorithm and a non-deterministic seed.</summary> |
| 18 | + /// <returns>A new, non-thread-safe random source.</returns> |
| 19 | + public static IRandom Create() |
| 20 | + { |
| 21 | + return new NRandomAdapter(NRandom.RandomEx.Create()); |
| 22 | + } |
| 23 | + |
| 24 | + /// <summary>Creates a reproducible random source using the default algorithm and the given seed.</summary> |
| 25 | + /// <param name="seed">The seed for reproducible sequences.</param> |
| 26 | + /// <returns>A new, non-thread-safe random source.</returns> |
| 27 | + public static IRandom Create(uint seed) |
| 28 | + { |
| 29 | + return Create(RandomAlgorithmType.Xoshiro256, seed); |
| 30 | + } |
| 31 | + |
| 32 | + /// <summary>Creates a random source using the given algorithm and a non-deterministic seed.</summary> |
| 33 | + /// <param name="algorithm">The pseudo-random algorithm to use.</param> |
| 34 | + /// <returns>A new, non-thread-safe random source.</returns> |
| 35 | + public static IRandom Create(RandomAlgorithmType algorithm) |
| 36 | + { |
| 37 | + return new NRandomAdapter(NewGenerator(algorithm)); |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary>Creates a reproducible random source using the given algorithm and seed.</summary> |
| 41 | + /// <param name="algorithm">The pseudo-random algorithm to use.</param> |
| 42 | + /// <param name="seed">The seed for reproducible sequences.</param> |
| 43 | + /// <returns>A new, non-thread-safe random source.</returns> |
| 44 | + public static IRandom Create(RandomAlgorithmType algorithm, uint seed) |
| 45 | + { |
| 46 | + var generator = NewGenerator(algorithm); |
| 47 | + generator.InitState(seed); |
| 48 | + |
| 49 | + return new NRandomAdapter(generator); |
| 50 | + } |
| 51 | + |
| 52 | + private static NRandom.IRandom NewGenerator(RandomAlgorithmType algorithm) |
| 53 | + { |
| 54 | + return algorithm switch |
| 55 | + { |
| 56 | + RandomAlgorithmType.Xoshiro256 => new NRandom.Xoshiro256StarStarRandom(), |
| 57 | + RandomAlgorithmType.Xoshiro128 => new NRandom.Xoshiro128StarStarRandom(), |
| 58 | + RandomAlgorithmType.Pcg32 => new NRandom.Pcg32Random(), |
| 59 | + RandomAlgorithmType.SplitMix64 => new NRandom.SplitMix64Random(), |
| 60 | + RandomAlgorithmType.MersenneTwister => new NRandom.MersenneTwisterRandom(), |
| 61 | + RandomAlgorithmType.ChaCha => new NRandom.ChaChaRandom(ChaChaRounds), |
| 62 | + _ => throw new ArgumentOutOfRangeException(nameof(algorithm), algorithm, null), |
| 63 | + }; |
| 64 | + } |
| 65 | +} |
0 commit comments