using System;
using System.Collections.Generic;
namespace TBL.CSharp.Utilities.Random.Generator
{
///
/// 已知序列的随机数生成器
/// 用于固定结果的演出教学等特殊用途
///
public class KnownSeriesGenerator : IRandom
{
///
/// 已经生成的随机数计数
///
private long _numberGenerated;
///
/// 已知序列
///
private readonly Queue _series;
///
/// 构造
///
/// 已知序列
/// 已知序列为空
public KnownSeriesGenerator(params int[] series)
{
if (series == null)
throw new ArgumentNullException(nameof(series));
_series = new Queue();
foreach (var num in series)
_series.Enqueue(num);
}
public int NextRaw()
{
var num = _series.Dequeue();
_series.Enqueue(num);
++_numberGenerated;
return num;
}
public Seed DumpSeed() => new Seed(_numberGenerated, _series);
public void RestoreBySeed(Seed seed)
{
_series.Clear();
foreach (var num in seed.KnownSeries)
_series.Enqueue(num);
_numberGenerated = seed.NumberGenerated;
}
///
/// 种子
///
public readonly struct Seed
{
///
/// 已经生成的随机数计数
///
public readonly long NumberGenerated;
///
/// 已知序列
///
public readonly int[] KnownSeries;
///
/// 构造
///
public Seed(long numberGenerated, Queue knownSeries)
{
NumberGenerated = numberGenerated;
KnownSeries = knownSeries.ToArray();
}
}
}
}