using System; namespace TBL.CSharp.Utilities.Random.Generator { /// /// 简易随机数生成器 /// 用于生成低质量的随机数;表现在不同设备上可以做到统一 /// public class SimpleGenerator : IRandom { /// /// 种子值 /// private int _seed; /// /// 从种子构造 /// /// 种子值 public SimpleGenerator(int seed) => RestoreBySeed(new Seed(seed)); public int NextRaw() { _seed++; var a = _seed * 15485863; return a * a * a % 2038074743; } /// /// 获取一个 0.0 ~ 1.0 之间的随机数 /// public double NextDouble() { _seed++; var a = _seed * 15485863; return Math.Abs((a * a * a % 2038074743) / 2038074743.0f); } public Seed DumpSeed() => new Seed(_seed); public void RestoreBySeed(Seed seed) => _seed = seed.SeedValue; /// /// 种子 /// public readonly struct Seed { /// /// 种子值 /// public readonly int SeedValue; /// /// 构造 /// public Seed(int seed) { SeedValue = seed; } } } }