30 lines
931 B
C#
30 lines
931 B
C#
using System;
|
|
|
|
namespace KernelPanic.Run
|
|
{
|
|
/// <summary>Retains System.Random's existing sequence, with a portable seed/cursor checkpoint.</summary>
|
|
public sealed class ReplayableRandom
|
|
{
|
|
private Random _random;
|
|
public int Seed { get; }
|
|
public int Draws { get; private set; }
|
|
|
|
public ReplayableRandom(int seed, int draws = 0)
|
|
{
|
|
if (draws < 0 || draws > 10000000) throw new ArgumentOutOfRangeException(nameof(draws));
|
|
Seed = seed;
|
|
_random = new Random(seed);
|
|
for (int i = 0; i < draws; i++) _random.Next();
|
|
Draws = draws;
|
|
}
|
|
|
|
public int Next(int min, int max)
|
|
{
|
|
// Large-range Next consumes two samples; combat/shop ranges use one.
|
|
int value = _random.Next(min, max);
|
|
Draws += (long)max - min > int.MaxValue ? 2 : 1;
|
|
return value;
|
|
}
|
|
}
|
|
}
|