| | | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | | 3 | | // See the LICENSE file in the project root for more information. |
| | | 4 | | |
| | | 5 | | using System; |
| | | 6 | | using System.Runtime.CompilerServices; |
| | | 7 | | using Unity.Collections; |
| | | 8 | | using Unity.Collections.LowLevel.Unsafe; |
| | | 9 | | using Unity.Mathematics; |
| | | 10 | | |
| | | 11 | | namespace GDX.Mathematics.Random |
| | | 12 | | { |
| | | 13 | | // ReSharper disable CommentTypo |
| | | 14 | | /// <summary> |
| | | 15 | | /// Generates pseudorandom values based on the WELL1024a algorithm. You must <see cref="Dispose" /> manually. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <remarks> |
| | | 18 | | /// Primarily based on the work of <a href="http://lomont.org/papers/2008/Lomont_PRNG_2008.pdf">Chris Lomont</a> |
| | | 19 | | /// accessed on 2021-04-23. |
| | | 20 | | /// Additional understanding from |
| | | 21 | | /// <a href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/lfsr04.pdf">Francois Panneton and Pierre L`Ecuyer |
| | | 22 | | /// accessed on 2021-04-23. |
| | | 23 | | /// </remarks> |
| | | 24 | | // ReSharper restore CommentTypo |
| | | 25 | | [VisualScriptingCompatible(4)] |
| | | 26 | | #pragma warning disable IDE1006 |
| | | 27 | | // ReSharper disable once InconsistentNaming |
| | | 28 | | public unsafe struct WELL1024a : IRandomProvider, IEquatable<WELL1024a>, IDisposable |
| | | 29 | | #pragma warning restore IDE1006 |
| | | 30 | | { |
| | | 31 | | /// <summary> |
| | | 32 | | /// The state array of the well. |
| | | 33 | | /// </summary> |
| | | 34 | | public NativeArray<uint> State; |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// A copy of the original seed used to initialize the <see cref="WELL1024a" />. |
| | | 38 | | /// </summary> |
| | | 39 | | public readonly uint OriginalSeed; |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// The number of times that this well has been sampled. |
| | | 43 | | /// </summary> |
| | | 44 | | public uint SampleCount; |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// The current index of use for the well array. |
| | | 48 | | /// </summary> |
| | | 49 | | /// <remarks>CAUTION! Changing this will alter the understanding of the data.</remarks> |
| | | 50 | | public byte Index; |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Creates a new pseudorandom number generator with the given <paramref name="seed" />. |
| | | 54 | | /// </summary> |
| | | 55 | | /// <remarks> |
| | | 56 | | /// The <paramref name="seed" /> will have its sign stripped and stored as such in |
| | | 57 | | /// <see cref="OriginalSeed" />. |
| | | 58 | | /// </remarks> |
| | | 59 | | /// <param name="seed">A <see cref="int" /> value to use as a seed.</param> |
| | | 60 | | public WELL1024a(int seed) |
| | 1 | 61 | | { |
| | 1 | 62 | | OriginalSeed = (uint)math.abs(seed); |
| | | 63 | | |
| | | 64 | | // Initialize |
| | 1 | 65 | | Index = 0; |
| | 1 | 66 | | SampleCount = 0; |
| | 1 | 67 | | State = new NativeArray<uint>(32, Allocator.Persistent); |
| | 1 | 68 | | uint* ptr = (uint*)State.GetUnsafePtr(); |
| | 1 | 69 | | ptr[0] = OriginalSeed & 4294967295u; |
| | 64 | 70 | | for (int i = 1; i < 32; ++i) |
| | 31 | 71 | | { |
| | 31 | 72 | | ptr[i] = (69069u * ptr[i - 1]) & 4294967295u; |
| | 31 | 73 | | } |
| | 1 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Creates a new pseudorandom number generator with the given <paramref name="seed" />. |
| | | 78 | | /// </summary> |
| | | 79 | | /// <param name="seed">A <see cref="uint" /> value to use as a seed.</param> |
| | | 80 | | public WELL1024a(uint seed) |
| | 3 | 81 | | { |
| | 3 | 82 | | OriginalSeed = seed; |
| | | 83 | | |
| | | 84 | | // Initialize |
| | 3 | 85 | | Index = 0; |
| | 3 | 86 | | SampleCount = 0; |
| | 3 | 87 | | State = new NativeArray<uint>(32, Allocator.Persistent); |
| | 3 | 88 | | uint* ptr = (uint*)State.GetUnsafePtr(); |
| | 3 | 89 | | ptr[0] = OriginalSeed & 4294967295u; |
| | 192 | 90 | | for (int i = 1; i < 32; ++i) |
| | 93 | 91 | | { |
| | 93 | 92 | | ptr[i] = (69069u * ptr[i - 1]) & 4294967295u; |
| | 93 | 93 | | } |
| | 3 | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Creates a new pseudorandom number generator with the given <paramref name="seed" />. |
| | | 98 | | /// </summary> |
| | | 99 | | /// <remarks> |
| | | 100 | | /// The created hashcode will have its sign stripped and stored as such in |
| | | 101 | | /// <see cref="OriginalSeed" />. |
| | | 102 | | /// </remarks> |
| | | 103 | | /// <param name="seed">A <see cref="string" /> to create a <see cref="uint" /> seed from.</param> |
| | | 104 | | /// <param name="forceUpperCase"> |
| | | 105 | | /// Should the generated hashcode used as the seed be generated from an uppercase version of |
| | | 106 | | /// the <paramref name="seed" />. |
| | | 107 | | /// </param> |
| | | 108 | | public WELL1024a(string seed, bool forceUpperCase = true) |
| | 32 | 109 | | { |
| | 32 | 110 | | if (forceUpperCase) |
| | 30 | 111 | | { |
| | 30 | 112 | | OriginalSeed = (uint)math.abs(seed.GetStableUpperCaseHashCode()); |
| | 30 | 113 | | } |
| | | 114 | | else |
| | 2 | 115 | | { |
| | 2 | 116 | | OriginalSeed = (uint)math.abs(seed.GetStableHashCode()); |
| | 2 | 117 | | } |
| | | 118 | | |
| | | 119 | | // Initialize |
| | 32 | 120 | | Index = 0; |
| | 32 | 121 | | SampleCount = 0; |
| | 32 | 122 | | State = new NativeArray<uint>(32, Allocator.Persistent); |
| | 32 | 123 | | uint* ptr = (uint*)State.GetUnsafePtr(); |
| | 32 | 124 | | ptr[0] = OriginalSeed & 4294967295u; |
| | 2048 | 125 | | for (int i = 1; i < 32; ++i) |
| | 992 | 126 | | { |
| | 992 | 127 | | ptr[i] = (69069u * ptr[i - 1]) & 4294967295u; |
| | 992 | 128 | | } |
| | 32 | 129 | | } |
| | | 130 | | |
| | | 131 | | /// <summary> |
| | | 132 | | /// Create a pseudorandom number generator from a <paramref name="restoreState" />. |
| | | 133 | | /// </summary> |
| | | 134 | | /// <param name="restoreState">A saved <see cref="WELL1024a" /> state.</param> |
| | | 135 | | public WELL1024a(WellState restoreState) |
| | 2 | 136 | | { |
| | 2 | 137 | | OriginalSeed = restoreState.Seed; |
| | 2 | 138 | | Index = restoreState.Index; |
| | | 139 | | // Create new native array |
| | 2 | 140 | | State = new NativeArray<uint>(32, Allocator.Persistent); |
| | 2 | 141 | | uint* ptr = (uint*)State.GetUnsafePtr(); |
| | 2 | 142 | | uint* restoreStatePtr = (uint*)restoreState.State.GetUnsafePtr(); |
| | 132 | 143 | | for (int i = 0; i < 32; i++) |
| | 64 | 144 | | { |
| | 64 | 145 | | ptr[i] = restoreStatePtr[i]; |
| | 64 | 146 | | } |
| | | 147 | | |
| | 2 | 148 | | SampleCount = restoreState.Count; |
| | 2 | 149 | | } |
| | | 150 | | |
| | | 151 | | /// <summary> |
| | | 152 | | /// Get a <see cref="WellState" /> for the <see cref="WELL1024a" />. |
| | | 153 | | /// </summary> |
| | | 154 | | /// <remarks>Useful to save and restore the state of the <see cref="WELL1024a" />.</remarks> |
| | | 155 | | /// <returns></returns> |
| | | 156 | | public WellState GetState() |
| | 2 | 157 | | { |
| | 2 | 158 | | return new WellState { Index = Index, State = State, Seed = OriginalSeed, Count = SampleCount }; |
| | 2 | 159 | | } |
| | | 160 | | |
| | | 161 | | /// <inheritdoc cref="IRandomProvider.NextBoolean" /> |
| | | 162 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 163 | | public bool NextBoolean(float chance = 0.5f) |
| | 3 | 164 | | { |
| | 3 | 165 | | return Sample() <= chance; |
| | 3 | 166 | | } |
| | | 167 | | |
| | | 168 | | /// <inheritdoc cref="IRandomProvider.NextBytes" /> |
| | | 169 | | public void NextBytes(byte[] buffer) |
| | 4 | 170 | | { |
| | 4 | 171 | | int bufLen = buffer.Length; |
| | 68 | 172 | | for (int idx = 0; idx < bufLen; ++idx) |
| | 30 | 173 | | { |
| | 30 | 174 | | buffer[idx] = (byte)NextInteger(0, 256); |
| | 30 | 175 | | } |
| | 4 | 176 | | } |
| | | 177 | | |
| | | 178 | | /// <inheritdoc cref="IRandomProvider.NextDouble" /> |
| | | 179 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 180 | | public double NextDouble(double minValue = 0d, double maxValue = 1d) |
| | 67 | 181 | | { |
| | 67 | 182 | | return Range.GetDouble(Sample(), minValue, maxValue); |
| | 67 | 183 | | } |
| | | 184 | | |
| | | 185 | | /// <inheritdoc cref="IRandomProvider.NextInteger" /> |
| | | 186 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 187 | | public int NextInteger(int minValue = 0, int maxValue = int.MaxValue) |
| | 124 | 188 | | { |
| | 124 | 189 | | return Range.GetInteger(Sample(), minValue, maxValue); |
| | 124 | 190 | | } |
| | | 191 | | |
| | | 192 | | /// <inheritdoc cref="IRandomProvider.NextIntegerExclusive" /> |
| | | 193 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 194 | | public int NextIntegerExclusive(int minValue = 0, int maxValue = int.MaxValue) |
| | 26 | 195 | | { |
| | 26 | 196 | | return Range.GetInteger(Sample(), minValue + 1, maxValue - 1); |
| | 26 | 197 | | } |
| | | 198 | | |
| | | 199 | | /// <inheritdoc cref="IRandomProvider.NextSingle" /> |
| | | 200 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 201 | | public float NextSingle(float minValue = 0f, float maxValue = 1f) |
| | 65 | 202 | | { |
| | 65 | 203 | | return Range.GetSingle(Sample(), minValue, maxValue); |
| | 65 | 204 | | } |
| | | 205 | | |
| | | 206 | | /// <inheritdoc cref="IRandomProvider.NextUnsignedInteger" /> |
| | | 207 | | public uint NextUnsignedInteger(uint minValue = uint.MinValue, uint maxValue = uint.MaxValue) |
| | 65 | 208 | | { |
| | 65 | 209 | | return Range.GetUnsignedInteger(Sample(), minValue, maxValue); |
| | 65 | 210 | | } |
| | | 211 | | |
| | | 212 | | /// <inheritdoc cref="IRandomProvider.NextUnsignedIntegerExclusive" /> |
| | | 213 | | public uint NextUnsignedIntegerExclusive(uint minValue = uint.MinValue, uint maxValue = uint.MaxValue) |
| | 2 | 214 | | { |
| | 2 | 215 | | return Range.GetUnsignedInteger(Sample(), minValue + 1, maxValue - 1); |
| | 2 | 216 | | } |
| | | 217 | | |
| | | 218 | | /// <inheritdoc cref="IRandomProvider.Sample" /> |
| | | 219 | | public double Sample() |
| | 355 | 220 | | { |
| | 355 | 221 | | SampleCount++; |
| | | 222 | | |
| | 355 | 223 | | uint* ptr = (uint*)State.GetUnsafePtr(); |
| | 355 | 224 | | uint a = ptr[(int)((Index + 3u) & 31u)]; |
| | 355 | 225 | | uint z1 = ptr[Index] ^ a ^ (a >> 8); |
| | 355 | 226 | | uint b = ptr[(int)((Index + 24u) & 31u)]; |
| | 355 | 227 | | uint c = ptr[(int)((Index + 10u) & 31u)]; |
| | 355 | 228 | | uint z2 = b ^ (b << 19) ^ c ^ (c << 14); |
| | | 229 | | |
| | 355 | 230 | | ptr[Index] = z1 ^ z2; |
| | 355 | 231 | | uint d = ptr[(int)((Index + 31u) & 31u)]; |
| | 355 | 232 | | ptr[(int)((Index + 31u) & 31u)] = d ^ (d << 11) ^ z1 ^ (z1 << 7) ^ z2 ^ (z2 << 13); |
| | 355 | 233 | | Index = (byte)((Index + 31u) & 31u); |
| | | 234 | | |
| | 355 | 235 | | return ptr[Index & 31u] * 2.32830643653869628906e-10d; |
| | 355 | 236 | | } |
| | | 237 | | |
| | | 238 | | /// <summary> |
| | | 239 | | /// A complete state of <see cref="WELL1024a" />. |
| | | 240 | | /// </summary> |
| | | 241 | | [Serializable] |
| | | 242 | | public struct WellState |
| | | 243 | | { |
| | | 244 | | /// <summary> |
| | | 245 | | /// The seed used to originally create the <see cref="WELL1024a" />. |
| | | 246 | | /// </summary> |
| | | 247 | | public uint Seed; |
| | | 248 | | |
| | | 249 | | /// <summary> |
| | | 250 | | /// The internal sample count. |
| | | 251 | | /// </summary> |
| | | 252 | | public uint Count; |
| | | 253 | | |
| | | 254 | | /// <summary> |
| | | 255 | | /// The internal state index. |
| | | 256 | | /// </summary> |
| | | 257 | | public byte Index; |
| | | 258 | | |
| | | 259 | | /// <summary> |
| | | 260 | | /// The internal state array. |
| | | 261 | | /// </summary> |
| | | 262 | | public NativeArray<uint> State; |
| | | 263 | | } |
| | | 264 | | |
| | | 265 | | /// <summary> |
| | | 266 | | /// Disposes of the native allocations. |
| | | 267 | | /// </summary> |
| | | 268 | | public void Dispose() |
| | 36 | 269 | | { |
| | 36 | 270 | | State.Dispose(); |
| | 36 | 271 | | } |
| | | 272 | | |
| | | 273 | | /// <summary> |
| | | 274 | | /// Is one <see cref="WELL1024a" /> the same as the <paramref name="other" />. |
| | | 275 | | /// </summary> |
| | | 276 | | /// <param name="other">The <see cref="WELL1024a" /> to compare with.</param> |
| | | 277 | | /// <returns>true/false if they are fundamentally the same.</returns> |
| | | 278 | | public bool Equals(ref WELL1024a other) |
| | 1 | 279 | | { |
| | 1 | 280 | | return OriginalSeed == other.OriginalSeed && |
| | | 281 | | Index == other.Index && |
| | | 282 | | SampleCount == other.SampleCount; |
| | 1 | 283 | | } |
| | | 284 | | |
| | | 285 | | /// <summary> |
| | | 286 | | /// Is one <see cref="WELL1024a" /> the same as the <paramref name="other" />. |
| | | 287 | | /// </summary> |
| | | 288 | | /// <remarks> |
| | | 289 | | /// Avoid using this format for comparison as it copies the data, where as |
| | | 290 | | /// <see cref="Equals(ref GDX.Mathematics.Random.WELL1024a)" /> does not. |
| | | 291 | | /// </remarks> |
| | | 292 | | /// <param name="other">The <see cref="WELL1024a" /> to compare with.</param> |
| | | 293 | | /// <returns>true/false if they are fundamentally the same.</returns> |
| | | 294 | | public bool Equals(WELL1024a other) |
| | 1 | 295 | | { |
| | 1 | 296 | | return OriginalSeed == other.OriginalSeed && |
| | | 297 | | Index == other.Index && |
| | | 298 | | SampleCount == other.SampleCount; |
| | 1 | 299 | | } |
| | | 300 | | |
| | | 301 | | /// <summary> |
| | | 302 | | /// Determines if the provided <paramref name="obj" />'s hash code is equal to this <see cref="WELL1024a" /> |
| | | 303 | | /// <see cref="GetHashCode" />. |
| | | 304 | | /// </summary> |
| | | 305 | | /// <remarks> |
| | | 306 | | /// This doesnt preclude other objects of different types from having the same hashcode. |
| | | 307 | | /// </remarks> |
| | | 308 | | /// <param name="obj">The <see cref="object" /> to compare hash codes with.</param> |
| | | 309 | | /// <returns>true/false if the hash codes match.</returns> |
| | | 310 | | public override bool Equals(object obj) |
| | 1 | 311 | | { |
| | 1 | 312 | | return obj != null && GetHashCode() == obj.GetHashCode(); |
| | 1 | 313 | | } |
| | | 314 | | |
| | | 315 | | /// <summary> |
| | | 316 | | /// Generate a hash code value for the given <see cref="WELL1024a" /> at its current state. |
| | | 317 | | /// </summary> |
| | | 318 | | /// <returns>The hash code value.</returns> |
| | | 319 | | public override int GetHashCode() |
| | 2 | 320 | | { |
| | | 321 | | unchecked |
| | 2 | 322 | | { |
| | 2 | 323 | | int hashCode = (int)OriginalSeed; |
| | 2 | 324 | | hashCode = (hashCode * 397) ^ Index; |
| | 2 | 325 | | hashCode = (hashCode * 397) ^ (int)SampleCount; |
| | 2 | 326 | | return hashCode; |
| | | 327 | | } |
| | 2 | 328 | | } |
| | | 329 | | } |
| | | 330 | | } |