1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- using System;
- namespace TBL.CSharp.Utilities.Random.Generator
- {
- /// <summary>
- /// 简易随机数生成器
- /// <para>用于生成低质量的随机数;表现在不同设备上可以做到统一</para>
- /// </summary>
- public class SimpleGenerator : IRandom<SimpleGenerator.Seed>
- {
- /// <summary>
- /// 种子值
- /// </summary>
- private int _seed;
- /// <summary>
- /// 从种子构造
- /// </summary>
- /// <param name="seed">种子值</param>
- public SimpleGenerator(int seed) => RestoreBySeed(new Seed(seed));
- public int NextRaw()
- {
- _seed++;
- var a = _seed * 15485863;
- return a * a * a % 2038074743;
- }
- /// <summary>
- /// 获取一个 <c>0.0</c> ~ <c>1.0</c> 之间的随机数
- /// </summary>
- 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;
- /// <summary>
- /// 种子
- /// </summary>
- public readonly struct Seed
- {
- /// <summary>
- /// 种子值
- /// </summary>
- public readonly int SeedValue;
- /// <summary>
- /// 构造
- /// </summary>
- public Seed(int seed)
- {
- SeedValue = seed;
- }
- }
- }
- }
|