| | | 1 | | using System; |
| | | 2 | | using System.Runtime.CompilerServices; |
| | | 3 | | |
| | | 4 | | namespace FixedMathSharp.Utility |
| | | 5 | | { |
| | | 6 | | /// <summary> |
| | | 7 | | /// Fast, seedable, deterministic RNG suitable for lockstep sims and map gen. |
| | | 8 | | /// Uses xoroshiro128++ with splitmix64 seeding. No allocations, no time/GUID. |
| | | 9 | | /// </summary> |
| | | 10 | | public struct DeterministicRandom |
| | | 11 | | { |
| | | 12 | | // xoroshiro128++ state |
| | | 13 | | private ulong _s0; |
| | | 14 | | private ulong _s1; |
| | | 15 | | |
| | | 16 | | #region Construction / Seeding |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Initializes a new instance of the DeterministicRandom class using the specified seed value. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <remarks> |
| | | 22 | | /// This constructor expands the provided seed into the internal state required for deterministic random number |
| | | 23 | | /// The generated sequence is fully determined by the seed value. |
| | | 24 | | /// </remarks> |
| | | 25 | | /// <param name="seed"> |
| | | 26 | | /// The initial seed value used to generate the internal state. |
| | | 27 | | /// Using the same seed will produce the same sequence of random numbers. |
| | | 28 | | /// </param> |
| | | 29 | | public DeterministicRandom(ulong seed) |
| | 26 | 30 | | { |
| | | 31 | | // Expand a single seed into two 64-bit state words via splitmix64. |
| | 26 | 32 | | _s0 = SplitMix64(ref seed); |
| | 26 | 33 | | _s1 = SplitMix64(ref seed); |
| | | 34 | | |
| | | 35 | | // xoroshiro requires non-zero state; repair pathological seed. |
| | 26 | 36 | | if (_s0 == 0UL && _s1 == 0UL) |
| | 0 | 37 | | _s1 = 0x9E3779B97F4A7C15UL; |
| | 26 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Create a stream deterministically |
| | | 42 | | /// Derived from (worldSeed, featureKey[,index]). |
| | | 43 | | /// </summary> |
| | | 44 | | public static DeterministicRandom FromWorldFeature(ulong worldSeed, ulong featureKey, ulong index = 0) |
| | 4 | 45 | | { |
| | | 46 | | // Simple reversible mix (swap for a stronger mix if required). |
| | 4 | 47 | | ulong seed = Mix64(worldSeed, featureKey); |
| | 4 | 48 | | seed = Mix64(seed, index); |
| | 4 | 49 | | return new DeterministicRandom(seed); |
| | 4 | 50 | | } |
| | | 51 | | |
| | | 52 | | #endregion |
| | | 53 | | |
| | | 54 | | #region Core PRNG |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// xoroshiro128++ next 64 bits. |
| | | 58 | | /// </summary> |
| | | 59 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 60 | | public ulong NextU64() |
| | 51883 | 61 | | { |
| | 103766 | 62 | | ulong s0 = _s0, s1 = _s1; |
| | 51883 | 63 | | ulong result = RotL(s0 + s1, 17) + s0; |
| | | 64 | | |
| | 51883 | 65 | | s1 ^= s0; |
| | 51883 | 66 | | _s0 = RotL(s0, 49) ^ s1 ^ (s1 << 21); // a,b |
| | 51883 | 67 | | _s1 = RotL(s1, 28); // c |
| | | 68 | | |
| | 51883 | 69 | | return result; |
| | 51883 | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Next non-negative Int32 in [0, int.MaxValue]. |
| | | 74 | | /// </summary> |
| | | 75 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 76 | | public int Next() |
| | 1004 | 77 | | { |
| | | 78 | | // Take high bits for better quality; mask to 31 bits non-negative. |
| | 1004 | 79 | | return (int)(NextU64() >> 33); |
| | 1004 | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Unbiased int in [0, maxExclusive). |
| | | 84 | | /// </summary> |
| | | 85 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 86 | | public int Next(int maxExclusive) |
| | 30214 | 87 | | { |
| | 30214 | 88 | | return maxExclusive <= 0 |
| | 30214 | 89 | | ? throw new ArgumentOutOfRangeException(nameof(maxExclusive)) |
| | 30214 | 90 | | : (int)NextBounded((uint)maxExclusive); |
| | 30212 | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Unbiased int in [min, maxExclusive). |
| | | 95 | | /// </summary> |
| | | 96 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 97 | | public int Next(int minInclusive, int maxExclusive) |
| | 4101 | 98 | | { |
| | 4101 | 99 | | if (minInclusive >= maxExclusive) |
| | 3 | 100 | | throw new ArgumentException("min >= max"); |
| | 4098 | 101 | | uint range = (uint)(maxExclusive - minInclusive); |
| | 4098 | 102 | | return minInclusive + (int)NextBounded(range); |
| | 4098 | 103 | | } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Double in [0,1). |
| | | 107 | | /// </summary> |
| | | 108 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 109 | | public double NextDouble() |
| | 4100 | 110 | | { |
| | | 111 | | // 53 random bits -> [0,1) |
| | 4100 | 112 | | return (NextU64() >> 11) * (1.0 / (1UL << 53)); |
| | 4100 | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Fill span with random bytes. |
| | | 117 | | /// </summary> |
| | | 118 | | public void NextBytes(Span<byte> buffer) |
| | 13 | 119 | | { |
| | 13 | 120 | | int i = 0; |
| | 34 | 121 | | while (i + 8 <= buffer.Length) |
| | 21 | 122 | | { |
| | 21 | 123 | | ulong v = NextU64(); |
| | 21 | 124 | | Unsafe.WriteUnaligned(ref buffer[i], v); |
| | 21 | 125 | | i += 8; |
| | 21 | 126 | | } |
| | 13 | 127 | | if (i < buffer.Length) |
| | 10 | 128 | | { |
| | 10 | 129 | | ulong v = NextU64(); |
| | 47 | 130 | | while (i < buffer.Length) |
| | 37 | 131 | | { |
| | 37 | 132 | | buffer[i++] = (byte)v; |
| | 37 | 133 | | v >>= 8; |
| | 37 | 134 | | } |
| | 10 | 135 | | } |
| | 13 | 136 | | } |
| | | 137 | | |
| | | 138 | | #endregion |
| | | 139 | | |
| | | 140 | | #region Fixed64 helpers |
| | | 141 | | |
| | | 142 | | /// <summary> |
| | | 143 | | /// Random Fixed64 in [0,1). |
| | | 144 | | /// </summary> |
| | | 145 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 146 | | public Fixed64 NextFixed6401() |
| | 4096 | 147 | | { |
| | | 148 | | // Produce a raw value in [0, One.m_rawValue) |
| | 4096 | 149 | | ulong rawOne = (ulong)Fixed64.One.m_rawValue; |
| | 4096 | 150 | | ulong r = NextBounded(rawOne); |
| | 4096 | 151 | | return Fixed64.FromRaw((long)r); |
| | 4096 | 152 | | } |
| | | 153 | | |
| | | 154 | | /// <summary> |
| | | 155 | | /// Random Fixed64 in [0, maxExclusive). |
| | | 156 | | /// </summary> |
| | | 157 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 158 | | public Fixed64 NextFixed64(Fixed64 maxExclusive) |
| | 4098 | 159 | | { |
| | 4098 | 160 | | if (maxExclusive <= Fixed64.Zero) |
| | 2 | 161 | | throw new ArgumentOutOfRangeException(nameof(maxExclusive), "max must be > 0"); |
| | 4096 | 162 | | ulong rawMax = (ulong)maxExclusive.m_rawValue; |
| | 4096 | 163 | | ulong r = NextBounded(rawMax); |
| | 4096 | 164 | | return Fixed64.FromRaw((long)r); |
| | 4096 | 165 | | } |
| | | 166 | | |
| | | 167 | | /// <summary> |
| | | 168 | | /// Random Fixed64 in [minInclusive, maxExclusive). |
| | | 169 | | /// </summary> |
| | | 170 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 171 | | public Fixed64 NextFixed64(Fixed64 minInclusive, Fixed64 maxExclusive) |
| | 4100 | 172 | | { |
| | 4100 | 173 | | if (minInclusive >= maxExclusive) |
| | 2 | 174 | | throw new ArgumentException("min >= max"); |
| | 4098 | 175 | | ulong span = (ulong)(maxExclusive.m_rawValue - minInclusive.m_rawValue); |
| | 4098 | 176 | | ulong r = NextBounded(span); |
| | 4098 | 177 | | return Fixed64.FromRaw((long)r + minInclusive.m_rawValue); |
| | 4098 | 178 | | } |
| | | 179 | | |
| | | 180 | | #endregion |
| | | 181 | | |
| | | 182 | | #region Internals: unbiased range, splitmix64, mixing, rotations |
| | | 183 | | |
| | | 184 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 185 | | private ulong NextBounded(ulong bound) |
| | 46600 | 186 | | { |
| | | 187 | | // Rejection to avoid modulo bias. |
| | | 188 | | // threshold = 2^64 % bound, but expressed as (-bound) % bound |
| | 46600 | 189 | | ulong threshold = unchecked((ulong)-(long)bound) % bound; |
| | 46600 | 190 | | while (true) |
| | 46600 | 191 | | { |
| | 46600 | 192 | | ulong r = NextU64(); |
| | 46600 | 193 | | if (r >= threshold) |
| | 46600 | 194 | | return r % bound; |
| | 0 | 195 | | } |
| | 46600 | 196 | | } |
| | | 197 | | |
| | | 198 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 155649 | 199 | | private static ulong RotL(ulong x, int k) => (x << k) | (x >> (64 - k)); |
| | | 200 | | |
| | | 201 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 202 | | private static ulong SplitMix64(ref ulong state) |
| | 52 | 203 | | { |
| | 52 | 204 | | ulong z = (state += 0x9E3779B97F4A7C15UL); |
| | 52 | 205 | | z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9UL; |
| | 52 | 206 | | z = (z ^ (z >> 27)) * 0x94D049BB133111EBUL; |
| | 52 | 207 | | return z ^ (z >> 31); |
| | 52 | 208 | | } |
| | | 209 | | |
| | | 210 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 211 | | private static ulong Mix64(ulong a, ulong b) |
| | 8 | 212 | | { |
| | | 213 | | // Simple reversible mix (variant of splitmix finalizer). |
| | 8 | 214 | | ulong x = a ^ (b + 0x9E3779B97F4A7C15UL); |
| | 8 | 215 | | x = (x ^ (x >> 30)) * 0xBF58476D1CE4E5B9UL; |
| | 8 | 216 | | x = (x ^ (x >> 27)) * 0x94D049BB133111EBUL; |
| | 8 | 217 | | return x ^ (x >> 31); |
| | 8 | 218 | | } |
| | | 219 | | |
| | | 220 | | #endregion |
| | | 221 | | } |
| | | 222 | | } |