/ GUNRPG.Core / Combat / TrackedRandom.cs
TrackedRandom.cs
 1  namespace GUNRPG.Core.Combat;
 2  
 3  /// <summary>
 4  /// Random wrapper that tracks call count so deterministic state can be restored.
 5  /// </summary>
 6  public sealed class TrackedRandom : Random
 7  {
 8      public int Seed { get; }
 9      public int CallCount { get; private set; }
10  
11      public TrackedRandom(int seed, int callCount = 0) : base(seed)
12      {
13          Seed = seed;
14          if (callCount > 0)
15          {
16              Advance(callCount);
17          }
18      }
19  
20      private void Advance(int calls)
21      {
22          for (int i = 0; i < calls; i++)
23          {
24              base.Next();
25          }
26  
27          CallCount += calls;
28      }
29  
30      public override int Next()
31      {
32          CallCount++;
33          return base.Next();
34      }
35  
36      public override int Next(int maxValue)
37      {
38          CallCount++;
39          return base.Next(maxValue);
40      }
41  
42      public override int Next(int minValue, int maxValue)
43      {
44          CallCount++;
45          return base.Next(minValue, maxValue);
46      }
47  
48      public override double NextDouble()
49      {
50          CallCount++;
51          return base.NextDouble();
52      }
53  
54      public override void NextBytes(byte[] buffer)
55      {
56          CallCount++;
57          base.NextBytes(buffer);
58      }
59  
60      public override void NextBytes(Span<byte> buffer)
61      {
62          CallCount++;
63          base.NextBytes(buffer);
64      }
65  }