| | | 1 | | using System; |
| | | 2 | | using System.Reflection; |
| | | 3 | | |
| | | 4 | | namespace AmbientServices; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// A class containing a random seed from which pseudorandom data of various types can be generated. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <remarks> |
| | | 10 | | /// <pitch>Reproducible pseudorandom data of many shapes (integers of every width, ranged values, skewed usually-small v |
| | | 11 | | /// <pledge> |
| | | 12 | | /// Determinism is the contract: two instances with the same seed making the same sequence of calls produce identical va |
| | | 13 | | /// Every value produced advances the seed; ranged forms return values in [inclusive lower, exclusive upper) and throw w |
| | | 14 | | /// Instances are mutable and not thread-safe; share streams across threads by giving each thread its own instance or se |
| | | 15 | | /// </pledge> |
| | | 16 | | /// <plan> |
| | | 17 | | /// The generator multiplies the incrementing seed by a large prime and fully reverses the bit order (log-n swap cascade |
| | | 18 | | /// Seed generation for <see cref="Next"/> mixes <see cref="Environment.TickCount"/>, an <see cref="AmbientStopwatch"/>' |
| | | 19 | | /// </plan> |
| | | 20 | | /// <priority> |
| | | 21 | | /// 1. Reproducibility over unpredictability: the entire state is one seed, a clone continues its source's stream exactl |
| | | 22 | | /// 2. Even distribution under small divisors over generator speed: values are produced by multiplying the seed by a lar |
| | | 23 | | /// 3. Cheap instances over thread safety: state is a single 64-bit seed with no synchronization, so instances are trivi |
| | | 24 | | /// </priority> |
| | | 25 | | /// Data generated using <see cref="Pseudorandom"/> should be more random than that generated by the system's <see cref= |
| | | 26 | | /// Additionally, instances constructed using <see cref="Pseudorandom.Pseudorandom(bool)"/> with true as a parameter or |
| | | 27 | | /// The random data generated from any particular seed is consistent, ie. given the same seed and sequence of property a |
| | | 28 | | /// This class should *not* ever be used for generating encryption keys of any type for production use. |
| | | 29 | | /// </remarks> |
| | | 30 | | public class Pseudorandom : IEquatable<Pseudorandom> |
| | | 31 | | { |
| | 2 | 32 | | private static readonly long _startTickCount = Environment.TickCount; |
| | 2 | 33 | | private static readonly AmbientStopwatch _stopwatch = AmbientStopwatch.StartNew(); // use this stopwatch to get see |
| | | 34 | | private static long _rotator; // interlocked |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Gets a thread-safe non-repeating seed to initialize a <see cref="Pseudorandom"/>, attempting to avoid using the |
| | | 38 | | /// Uses timing information including the high frequency performance counter from <see cref="System.Diagnostics.Stop |
| | | 39 | | /// </summary> |
| | 2 | 40 | | private static ulong NextGlobalSeed => (ulong)((_startTickCount + _stopwatch.ElapsedTicks) ^ System.Threading.Interl |
| | | 41 | | /// <summary> |
| | | 42 | | /// Gets a new <see cref="Pseudorandom"/> using a thread-safe seed generator. |
| | | 43 | | /// Getting even a single random number through this property is very efficient. |
| | | 44 | | /// </summary> |
| | 2 | 45 | | public static Pseudorandom Next => new((int)NextGlobalSeed); |
| | | 46 | | |
| | | 47 | | private ulong _seed; |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Constructs a pseudorandom with a zero seed. |
| | | 51 | | /// </summary> |
| | 2 | 52 | | public Pseudorandom() |
| | | 53 | | { |
| | 2 | 54 | | _seed = 0; |
| | 2 | 55 | | } |
| | | 56 | | /// <summary> |
| | | 57 | | /// Constructs a pseudorandom with the specified seed value. |
| | | 58 | | /// </summary> |
| | | 59 | | /// <param name="seed">The seed to use.</param> |
| | 2 | 60 | | public Pseudorandom(long seed) |
| | | 61 | | { |
| | 2 | 62 | | _seed = (ulong)seed; |
| | 2 | 63 | | } |
| | | 64 | | /// <summary> |
| | | 65 | | /// Constructs a pseudorandom with the specified seed value. |
| | | 66 | | /// </summary> |
| | | 67 | | /// <param name="seed">The seed to use.</param> |
| | | 68 | | [CLSCompliant(false)] |
| | 2 | 69 | | public Pseudorandom(ulong seed) |
| | | 70 | | { |
| | 2 | 71 | | _seed = seed; |
| | 2 | 72 | | } |
| | | 73 | | /// <summary> |
| | | 74 | | /// Clones this <see cref="Pseudorandom"/> such that identical calls on the clone will return the same pseudorandom |
| | | 75 | | /// </summary> |
| | | 76 | | /// <returns>A new <see cref="Pseudorandom"/> in the same state as this one.</returns> |
| | | 77 | | public Pseudorandom Clone() |
| | | 78 | | { |
| | 2 | 79 | | return new Pseudorandom(_seed); |
| | | 80 | | } |
| | | 81 | | /// <summary> |
| | | 82 | | /// Constructs a pseudorandom with a seed generated from the system time combined with a global rotating number. |
| | | 83 | | /// </summary> |
| | | 84 | | /// <param name="generateSeed">Whether or not to create a seed using the current system time combined with a global |
| | 2 | 85 | | public Pseudorandom(bool generateSeed) |
| | | 86 | | { |
| | 2 | 87 | | _seed = generateSeed ? (uint)NextGlobalSeed : 0; |
| | 2 | 88 | | } |
| | | 89 | | /// <summary> |
| | | 90 | | /// Gets the next <see cref="ulong"/> based on the current seed. Values will be roughly evenly distributed across a |
| | | 91 | | /// </summary> |
| | | 92 | | [CLSCompliant(false)] |
| | | 93 | | public ulong NextUInt64 |
| | | 94 | | { |
| | | 95 | | get |
| | | 96 | | { |
| | | 97 | | unchecked |
| | | 98 | | { |
| | 2 | 99 | | ulong x = ++_seed * 1_111_111_111_111_111_111UL; // note that this is a prime number (but not a m |
| | 2 | 100 | | x = (((x & 0xaaaaaaaaaaaaaaaa) >> 1) | ((x & 0x5555555555555555) << 1)); |
| | 2 | 101 | | x = (((x & 0xcccccccccccccccc) >> 2) | ((x & 0x3333333333333333) << 2)); |
| | 2 | 102 | | x = (((x & 0xf0f0f0f0f0f0f0f0) >> 4) | ((x & 0x0f0f0f0f0f0f0f0f) << 4)); |
| | 2 | 103 | | x = (((x & 0xff00ff00ff00ff00) >> 8) | ((x & 0x00ff00ff00ff00ff) << 8)); |
| | 2 | 104 | | x = (((x & 0xffff0000ffff0000) >> 16)| ((x & 0x0000ffff0000ffff) << 16)); |
| | 2 | 105 | | return ((x >> 32) | (x << 32)); |
| | | 106 | | } |
| | | 107 | | } |
| | | 108 | | } |
| | | 109 | | /// <summary> |
| | | 110 | | /// Gets the next <see cref="long"/> based on the current seed. Note that this may return a negative number. Value |
| | | 111 | | /// </summary> |
| | 2 | 112 | | public long NextInt64Signed => (long)NextUInt64 * NextSignMultiplier; |
| | | 113 | | /// <summary> |
| | | 114 | | /// Gets the next positive <see cref="long"/> based on the current seed. Values will be roughly evenly distributed |
| | | 115 | | /// </summary> |
| | 2 | 116 | | public long NextInt64 => (long)(NextUInt64 & 0x7fffffffffffffff); |
| | | 117 | | /// <summary> |
| | | 118 | | /// Gets the next positive <see cref="long"/> based on the current seed. Values will be roughly evenly distributed |
| | | 119 | | /// </summary> |
| | | 120 | | /// <param name="maxValue">The possible maximum value (exclusive).</param> |
| | | 121 | | /// <returns>A random value between zero (inclusive) and <paramref name="maxValue"/> (exclusive).</returns> |
| | | 122 | | public long NextInt64Ranged(long maxValue) |
| | | 123 | | { |
| | 2 | 124 | | return (long)((NextUInt64 & 0x7fffffffffffffffUL) % (ulong)maxValue); |
| | | 125 | | } |
| | | 126 | | /// <summary> |
| | | 127 | | /// Gets a random <see cref="long"/> in the specified range. Values will be roughly evenly distributed across all v |
| | | 128 | | /// </summary> |
| | | 129 | | /// <param name="lowerLimit">The lower limit for the number, inclusive.</param> |
| | | 130 | | /// <param name="upperLimit">The upper limit for the number, exclusive.</param> |
| | | 131 | | /// <returns>A pseudorandom number between <paramref name="lowerLimit"/> (inclusive) and <paramref name="upperLimit" |
| | | 132 | | public long NextInt64Ranged(long lowerLimit, long upperLimit) |
| | | 133 | | { |
| | 2 | 134 | | if (upperLimit < lowerLimit) throw new ArgumentException("The upper limit must be higher than the lower limit!", |
| | 2 | 135 | | ulong difference = (ulong)(upperLimit - lowerLimit); |
| | 2 | 136 | | return (difference == 0) ? lowerLimit : (lowerLimit + (long)(NextUInt64 % difference)); |
| | | 137 | | } |
| | | 138 | | /// <summary> |
| | | 139 | | /// Gets the next <see cref="uint"/> based on the current seed. Values will be roughly evenly distributed across al |
| | | 140 | | /// </summary> |
| | | 141 | | [CLSCompliant(false)] |
| | | 142 | | public uint NextUInt32 |
| | | 143 | | { |
| | | 144 | | get |
| | | 145 | | { |
| | | 146 | | unchecked |
| | | 147 | | { |
| | 2 | 148 | | uint x = (uint)(++_seed * 777_767_777); // note that this is a prime number (but not a mersenne prim |
| | 2 | 149 | | x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1)); |
| | 2 | 150 | | x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2)); |
| | 2 | 151 | | x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4)); |
| | 2 | 152 | | x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8)); |
| | 2 | 153 | | return ((x >> 16) | (x << 16)); |
| | | 154 | | } |
| | | 155 | | } |
| | | 156 | | } |
| | | 157 | | /// <summary> |
| | | 158 | | /// Gets the next <see cref="int"/> based on the current seed. Note that this may return a negative number. Values |
| | | 159 | | /// </summary> |
| | 2 | 160 | | public int NextInt32Signed => (int)NextUInt32 * NextSignMultiplier; |
| | | 161 | | /// <summary> |
| | | 162 | | /// Gets the next positive <see cref="int"/> based on the current seed. Values will be roughly evenly distributed a |
| | | 163 | | /// </summary> |
| | 2 | 164 | | public int NextInt32 => (int)(NextUInt32 & 0x7fffffff); |
| | | 165 | | /// <summary> |
| | | 166 | | /// Gets the next positive <see cref="int"/> based on the current seed. Values will be roughly evenly distributed a |
| | | 167 | | /// </summary> |
| | | 168 | | /// <param name="maxValue">The possible maximum value (exclusive).</param> |
| | | 169 | | /// <returns>The selected value.</returns> |
| | | 170 | | public int NextInt32Ranged(int maxValue) |
| | | 171 | | { |
| | 2 | 172 | | return (int)((NextUInt32 & 0x7fffffff) % maxValue); |
| | | 173 | | } |
| | | 174 | | /// <summary> |
| | | 175 | | /// Gets a random <see cref="int"/> in the specified range. Values will be roughly evenly distributed across all va |
| | | 176 | | /// </summary> |
| | | 177 | | /// <param name="lowerLimit">The lower limit for the number, inclusive.</param> |
| | | 178 | | /// <param name="upperLimit">The upper limit for the number, exclusive.</param> |
| | | 179 | | /// <returns>A pseudorandom number between <paramref name="lowerLimit"/> (inclusive) and <paramref name="upperLimit" |
| | | 180 | | public int NextInt32Ranged(int lowerLimit, int upperLimit) |
| | | 181 | | { |
| | 2 | 182 | | if (upperLimit < lowerLimit) throw new ArgumentException("The upper limit must be higher than the lower limit!", |
| | 2 | 183 | | uint difference = (uint)(upperLimit - lowerLimit); |
| | 2 | 184 | | return (difference == 0) ? lowerLimit : (lowerLimit + (int)(NextUInt32 % difference)); |
| | | 185 | | } |
| | | 186 | | |
| | | 187 | | |
| | | 188 | | |
| | | 189 | | |
| | | 190 | | /// <summary> |
| | | 191 | | /// Gets a random <see cref="uint"/> in the specified range. Values will be roughly evenly distributed across all v |
| | | 192 | | /// </summary> |
| | | 193 | | /// <param name="lowerLimit">The lower limit for the number, inclusive.</param> |
| | | 194 | | /// <param name="upperLimit">The upper limit for the number, exclusive.</param> |
| | | 195 | | /// <returns>A pseudorandom number between <paramref name="lowerLimit"/> (inclusive) and <paramref name="upperLimit" |
| | | 196 | | [CLSCompliant(false)] |
| | | 197 | | public uint NextUInt32Ranged(uint lowerLimit, uint upperLimit) |
| | | 198 | | { |
| | 2 | 199 | | if (upperLimit < lowerLimit) throw new ArgumentException("The upper limit must be higher than the lower limit!", |
| | 2 | 200 | | uint difference = upperLimit - lowerLimit; |
| | 2 | 201 | | return (difference == 0) ? lowerLimit : (lowerLimit + NextUInt32 % difference); |
| | | 202 | | } |
| | | 203 | | /// <summary> |
| | | 204 | | /// Gets a random signed <see cref="int"/> in the specified range. Values will be roughly evenly distributed across |
| | | 205 | | /// </summary> |
| | | 206 | | /// <param name="lowerLimit">The lower limit for the number, inclusive.</param> |
| | | 207 | | /// <param name="upperLimit">The upper limit for the number, exclusive.</param> |
| | | 208 | | /// <returns>A pseudorandom number between <paramref name="lowerLimit"/> (inclusive) and <paramref name="upperLimit" |
| | | 209 | | public int NextInt32SignedRanged(int lowerLimit, int upperLimit) |
| | | 210 | | { |
| | 2 | 211 | | if (upperLimit < lowerLimit) throw new ArgumentException("The upper limit must be higher than the lower limit!", |
| | 2 | 212 | | uint difference = (uint)(upperLimit - lowerLimit); |
| | 2 | 213 | | return (difference == 0) ? lowerLimit : (lowerLimit + (int)(NextUInt32 % difference)); |
| | | 214 | | } |
| | | 215 | | /// <summary> |
| | | 216 | | /// Gets a random <see cref="uint"/> that is usually small. All <see cref="uint"/> values are possible, but smaller |
| | | 217 | | /// </summary> |
| | | 218 | | [CLSCompliant(false)] |
| | 2 | 219 | | public uint NextUInt32UsuallySmall => NextUInt32RangedUsuallySmall(uint.MaxValue); |
| | | 220 | | /// <summary> |
| | | 221 | | /// Gets a random <see cref="int"/> that is usually small. All non-negative <see cref="int"/> values are possible, |
| | | 222 | | /// </summary> |
| | 2 | 223 | | public int NextInt32UsuallySmall => NextInt32RangedUsuallySmall(int.MaxValue); |
| | | 224 | | /// <summary> |
| | | 225 | | /// Gets a random signed <see cref="int"/> that is usually small. All <see cref="int"/> values are possible, but va |
| | | 226 | | /// </summary> |
| | 2 | 227 | | public int NextInt32SignedUsuallySmall => NextInt32SignedRangedUsuallySmall(int.MinValue, int.MaxValue, 8); |
| | | 228 | | /// <summary> |
| | | 229 | | /// Gets a random <see cref="uint"/> that is usually small. All <see cref="uint"/> values up to <paramref name="upp |
| | | 230 | | /// </summary> |
| | | 231 | | /// <param name="upperLimit">The upper limit (exclusive).</param> |
| | | 232 | | /// <param name="iterations">The number of iterations (more iterations means more preference for the small numbers). |
| | | 233 | | /// <returns>A random <see cref="uint"/> that is usually small.</returns> |
| | | 234 | | [CLSCompliant(false)] |
| | | 235 | | public uint NextUInt32RangedUsuallySmall(uint upperLimit, int iterations = 8) |
| | | 236 | | { |
| | 2 | 237 | | uint baseAdjuster = NextUInt32; |
| | 2 | 238 | | uint maxDivisor = (1U << iterations); |
| | 2 | 239 | | uint divisor = (iterations > 31) ? int.MaxValue : (1U + (baseAdjuster % maxDivisor)); |
| | 2 | 240 | | ushort downShift = (ushort)(baseAdjuster % (iterations)); |
| | 2 | 241 | | uint baseNumber = NextUInt32Ranged(0, upperLimit >> downShift); |
| | 2 | 242 | | return baseNumber / divisor; |
| | | 243 | | } |
| | | 244 | | /// <summary> |
| | | 245 | | /// Gets a random <see cref="int"/> that is usually small. All <see cref="int"/> values between <paramref name="low |
| | | 246 | | /// </summary> |
| | | 247 | | /// <param name="lowerLimit">The lower limit (inclusive).</param> |
| | | 248 | | /// <param name="upperLimit">The upper limit (exclusive).</param> |
| | | 249 | | /// <param name="iterations">The number of iterations (more iterations means more preference for the small numbers). |
| | | 250 | | /// <returns>A random <see cref="int"/> that is usually nearer to zero.</returns> |
| | | 251 | | public int NextInt32SignedRangedUsuallySmall(int lowerLimit, int upperLimit, int iterations = 8) |
| | | 252 | | { |
| | 2 | 253 | | if (upperLimit < lowerLimit) throw new ArgumentException("The upper limit must be higher than the lower limit!", |
| | | 254 | | // always positive? |
| | 2 | 255 | | if (lowerLimit >= 0) |
| | | 256 | | { |
| | 2 | 257 | | return (int)(lowerLimit + NextUInt32RangedUsuallySmall((uint)(upperLimit - lowerLimit), iterations)); |
| | | 258 | | } |
| | | 259 | | // always negative? |
| | 2 | 260 | | if (upperLimit <= 0) |
| | | 261 | | { |
| | 2 | 262 | | return (int)(upperLimit - 1 - NextUInt32RangedUsuallySmall((uint)(lowerLimit - upperLimit), iterations)); |
| | | 263 | | } |
| | 2 | 264 | | uint range = (uint)(upperLimit - lowerLimit); |
| | | 265 | | // pick a positive? |
| | 2 | 266 | | if ((NextUInt32 * NextUInt32) % range < upperLimit) |
| | | 267 | | { |
| | 2 | 268 | | return (int)NextUInt32RangedUsuallySmall((uint)upperLimit, iterations); |
| | | 269 | | } |
| | | 270 | | else // negative |
| | | 271 | | { |
| | 2 | 272 | | return -(int)NextUInt32RangedUsuallySmall((uint)(-lowerLimit), iterations) - 1; |
| | | 273 | | } |
| | | 274 | | } |
| | | 275 | | /// <summary> |
| | | 276 | | /// Gets a random <see cref="int"/> that is usually small. All non-negative <see cref="int"/> values up to <paramre |
| | | 277 | | /// </summary> |
| | | 278 | | /// <param name="upperLimit">The upper limit (exclusive).</param> |
| | | 279 | | /// <param name="iterations">The number of iterations (more iterations means more preference for the small numbers). |
| | | 280 | | /// <returns>A random <see cref="int"/> between zero and <paramref name="upperLimit"/> (exclusive), that is usually |
| | | 281 | | public int NextInt32RangedUsuallySmall(int upperLimit, int iterations = 8) |
| | | 282 | | { |
| | 2 | 283 | | return (int)NextUInt32RangedUsuallySmall((uint)upperLimit, iterations); |
| | | 284 | | } |
| | | 285 | | /// <summary> |
| | | 286 | | /// Gets a random <see cref="ulong"/> in the specified range. Values will be roughly evenly distributed across all |
| | | 287 | | /// </summary> |
| | | 288 | | /// <param name="lowerLimit">The lower limit for the number, inclusive.</param> |
| | | 289 | | /// <param name="upperLimit">The upper limit for the number, exclusive.</param> |
| | | 290 | | /// <returns>A pseudorandom number between <paramref name="lowerLimit"/> (inclusive) and <paramref name="upperLimit" |
| | | 291 | | [CLSCompliant(false)] |
| | | 292 | | public ulong NextUInt64Ranged(ulong lowerLimit, ulong upperLimit) |
| | | 293 | | { |
| | 2 | 294 | | if (upperLimit < lowerLimit) throw new ArgumentException("The upper limit must be higher than the lower limit!", |
| | 2 | 295 | | ulong difference = upperLimit - lowerLimit; |
| | 2 | 296 | | return (difference == 0) ? lowerLimit : (lowerLimit + NextUInt64 % difference); |
| | | 297 | | } |
| | | 298 | | /// <summary> |
| | | 299 | | /// Gets a random signed <see cref="long"/> in the specified range. Values will be roughly evenly distributed acros |
| | | 300 | | /// </summary> |
| | | 301 | | /// <param name="lowerLimit">The lower limit for the number, inclusive.</param> |
| | | 302 | | /// <param name="upperLimit">The upper limit for the number, exclusive.</param> |
| | | 303 | | /// <returns>A pseudorandom number between <paramref name="lowerLimit"/> (inclusive) and <paramref name="upperLimit" |
| | | 304 | | public long NextInt64SignedRanged(long lowerLimit, long upperLimit) |
| | | 305 | | { |
| | 2 | 306 | | if (upperLimit < lowerLimit) throw new ArgumentException("The upper limit must be higher than the lower limit!", |
| | 2 | 307 | | ulong difference = (ulong)(upperLimit - lowerLimit); |
| | 2 | 308 | | return (difference == 0) ? lowerLimit : (lowerLimit + (long)(NextUInt64 % difference)); |
| | | 309 | | } |
| | | 310 | | /// <summary> |
| | | 311 | | /// Gets a random <see cref="ulong"/> that is usually small. All <see cref="ulong"/> values are possible, but small |
| | | 312 | | /// </summary> |
| | | 313 | | [CLSCompliant(false)] |
| | 2 | 314 | | public ulong NextUInt64UsuallySmall => NextUInt64RangedUsuallySmall(ulong.MaxValue); |
| | | 315 | | /// <summary> |
| | | 316 | | /// Gets a random <see cref="long"/> that is usually small. All non-negative <see cref="long"/> values are possible |
| | | 317 | | /// </summary> |
| | 2 | 318 | | public long NextInt64UsuallySmall => NextInt64RangedUsuallySmall(long.MaxValue); |
| | | 319 | | /// <summary> |
| | | 320 | | /// Gets a random <see cref="long"/> that is usually small. All <see cref="long"/> values are possible, but values |
| | | 321 | | /// </summary> |
| | 2 | 322 | | public long NextInt64SignedUsuallySmall => NextInt64SignedRangedUsuallySmall(long.MinValue, long.MaxValue); |
| | | 323 | | /// <summary> |
| | | 324 | | /// Gets a random <see cref="ulong"/> that is usually small. All <see cref="ulong"/> values up to <paramref name="u |
| | | 325 | | /// </summary> |
| | | 326 | | /// <param name="upperLimit">The upper limit (exclusive).</param> |
| | | 327 | | /// <param name="iterations">The number of iterations (more iterations means more preference for the small numbers). |
| | | 328 | | /// <returns>A random <see cref="ulong"/> that is usually small.</returns> |
| | | 329 | | [CLSCompliant(false)] |
| | | 330 | | public ulong NextUInt64RangedUsuallySmall(ulong upperLimit, int iterations = 20) |
| | | 331 | | { |
| | 2 | 332 | | ulong baseAdjuster = NextUInt64; |
| | 2 | 333 | | uint maxDivisor = (1U << iterations); |
| | 2 | 334 | | ulong divisor = (iterations > 63) ? long.MaxValue : (1U + (baseAdjuster % maxDivisor)); |
| | 2 | 335 | | ushort downShift = (ushort)(baseAdjuster % (uint)iterations); |
| | 2 | 336 | | ulong baseNumber = NextUInt64Ranged(0, upperLimit >> downShift); |
| | 2 | 337 | | return baseNumber / divisor; |
| | | 338 | | } |
| | | 339 | | /// <summary> |
| | | 340 | | /// Gets a random <see cref="long"/> that is usually small. All <see cref="int"/> values between <paramref name="lo |
| | | 341 | | /// </summary> |
| | | 342 | | /// <param name="lowerLimit">The lower limit (inclusive).</param> |
| | | 343 | | /// <param name="upperLimit">The upper limit (exclusive).</param> |
| | | 344 | | /// <param name="iterations">The number of iterations (more iterations means more preference for the small numbers). |
| | | 345 | | /// <returns>A random <see cref="long"/> that is usually nearer to zero.</returns> |
| | | 346 | | public long NextInt64SignedRangedUsuallySmall(long lowerLimit, long upperLimit, int iterations = 20) |
| | | 347 | | { |
| | 2 | 348 | | if (upperLimit < lowerLimit) throw new ArgumentException("The upper limit must be higher than the lower limit!", |
| | | 349 | | // always positive? |
| | 2 | 350 | | if (lowerLimit >= 0) |
| | | 351 | | { |
| | 2 | 352 | | return lowerLimit + (long)NextUInt64RangedUsuallySmall((ulong)(upperLimit - lowerLimit), iterations); |
| | | 353 | | } |
| | | 354 | | // always negative? |
| | 2 | 355 | | if (upperLimit <= 0) |
| | | 356 | | { |
| | 2 | 357 | | return upperLimit - 1 - (long)NextUInt64RangedUsuallySmall((ulong)(lowerLimit - upperLimit), iterations); |
| | | 358 | | } |
| | 2 | 359 | | ulong range = (ulong)(upperLimit - lowerLimit); |
| | | 360 | | // pick a positive? |
| | 2 | 361 | | if ((NextUInt64 * NextUInt64) % range < (ulong)upperLimit) |
| | | 362 | | { |
| | 2 | 363 | | return (long)NextUInt64RangedUsuallySmall((ulong)upperLimit, iterations); |
| | | 364 | | } |
| | | 365 | | else // negative |
| | | 366 | | { |
| | 2 | 367 | | return -(long)NextUInt64RangedUsuallySmall((ulong)(-lowerLimit), iterations) - 1; |
| | | 368 | | } |
| | | 369 | | } |
| | | 370 | | /// <summary> |
| | | 371 | | /// Gets a random <see cref="long"/> that is usually small. All non-negative <see cref="long"/> values up to <param |
| | | 372 | | /// </summary> |
| | | 373 | | /// <param name="upperLimit">The upper limit (exclusive).</param> |
| | | 374 | | /// <param name="iterations">The number of iterations (more iterations means more preference for the small numbers). |
| | | 375 | | /// <returns>A random <see cref="long"/> between zero and <paramref name="upperLimit"/> (exclusive), that is usually |
| | | 376 | | public long NextInt64RangedUsuallySmall(long upperLimit, int iterations = 20) |
| | | 377 | | { |
| | 2 | 378 | | return (long)NextUInt64RangedUsuallySmall((ulong)upperLimit, iterations); |
| | | 379 | | } |
| | | 380 | | |
| | | 381 | | |
| | | 382 | | /// <summary> |
| | | 383 | | /// Gets a random sign multiplier. Values returned should be 1 roughly 50% of the time and -1 roughly 50% of the ti |
| | | 384 | | /// </summary> |
| | 2 | 385 | | public int NextSignMultiplier => (2 * (int)NextUInt32Ranged(0, 2)) - 1; |
| | | 386 | | /// <summary> |
| | | 387 | | /// Gets a random boolean, either true or false, with a roughly even distribution between those values. |
| | | 388 | | /// </summary> |
| | 2 | 389 | | public bool NextBoolean => NextUInt32Ranged(0, 2) == 0; |
| | | 390 | | /// <summary> |
| | | 391 | | /// Returns an array of the specified length populated with random values. |
| | | 392 | | /// </summary> |
| | | 393 | | /// <param name="length">The number of random bytes to put in the returned array.</param> |
| | | 394 | | /// <returns>An array of bytes of the specified length populated with random byte values.</returns> |
| | | 395 | | public byte[] NextNewBytes(int length) |
| | | 396 | | { |
| | 2 | 397 | | byte[] bytes = new byte[length]; |
| | 2 | 398 | | NextBytes(bytes); |
| | 2 | 399 | | return bytes; |
| | | 400 | | } |
| | | 401 | | /// <summary> |
| | | 402 | | /// Populates an existing byte array with random values. |
| | | 403 | | /// </summary> |
| | | 404 | | /// <param name="target">The target array of bytes.</param> |
| | | 405 | | /// <param name="offset">The offset in the byte array to begin filling with random bytes.</param> |
| | | 406 | | /// <param name="length">The number of random bytes to put in the returned array.</param> |
| | | 407 | | /// <returns>An array of bytes of the specified length populated with random byte values.</returns> |
| | | 408 | | public void NextBytes(byte[] target, int offset = 0, int length = -1) |
| | | 409 | | { |
| | | 410 | | #if NET5_0_OR_GREATER |
| | 2 | 411 | | ArgumentNullException.ThrowIfNull(target); |
| | | 412 | | #else |
| | | 413 | | if (target is null) throw new ArgumentNullException(nameof(target)); |
| | | 414 | | #endif |
| | 2 | 415 | | int index = 0; |
| | | 416 | | ulong rawData; |
| | 2 | 417 | | for (int endOffset = (length < 0 || offset + length > target.Length) ? target.Length : (offset + length); offset |
| | | 418 | | { |
| | 2 | 419 | | rawData = NextUInt32; |
| | 2 | 420 | | target[offset] = (byte)(rawData & 0xff); |
| | 2 | 421 | | if (offset + 1 < endOffset) target[offset + 1] = (byte)((rawData & 0xff00) >> 8); |
| | 2 | 422 | | if (offset + 2 < endOffset) target[offset + 2] = (byte)((rawData & 0xff0000) >> 16); |
| | 2 | 423 | | if (offset + 3 < endOffset) target[offset + 3] = (byte)((rawData & 0xff000000) >> 24); |
| | 2 | 424 | | if (offset + 4 < endOffset) target[offset + 4] = (byte)((rawData & 0xff00000000) >> 32); |
| | 2 | 425 | | if (offset + 5 < endOffset) target[offset + 5] = (byte)((rawData & 0xff0000000000) >> 40); |
| | 2 | 426 | | if (offset + 6 < endOffset) target[offset + 6] = (byte)((rawData & 0xff000000000000) >> 48); |
| | 2 | 427 | | if (offset + 7 < endOffset) target[offset + 7] = (byte)((rawData & 0xff00000000000000) >> 56); |
| | | 428 | | } |
| | 2 | 429 | | } |
| | | 430 | | /// <summary> |
| | | 431 | | /// Populates an existing byte span with random values. |
| | | 432 | | /// </summary> |
| | | 433 | | /// <param name="target">The target Span of bytes.</param> |
| | | 434 | | /// <param name="offset">The offset in the byte array to begin filling with random bytes.</param> |
| | | 435 | | /// <param name="length">The number of random bytes to put in the returned array.</param> |
| | | 436 | | /// <returns>An array of bytes of the specified length populated with random byte values.</returns> |
| | | 437 | | public void NextBytes(Span<byte> target, int offset = 0, int length = -1) |
| | | 438 | | { |
| | 2 | 439 | | int index = 0; |
| | | 440 | | ulong rawData; |
| | 2 | 441 | | for (int endOffset = (length < 0 || offset + length > target.Length) ? target.Length : (offset + length); offset |
| | | 442 | | { |
| | 2 | 443 | | rawData = NextUInt32; |
| | 2 | 444 | | target[offset] = (byte)(rawData & 0xff); |
| | 2 | 445 | | if (offset + 1 < endOffset) target[offset + 1] = (byte)((rawData & 0xff00) >> 8); |
| | 2 | 446 | | if (offset + 2 < endOffset) target[offset + 2] = (byte)((rawData & 0xff0000) >> 16); |
| | 2 | 447 | | if (offset + 3 < endOffset) target[offset + 3] = (byte)((rawData & 0xff000000) >> 24); |
| | 2 | 448 | | if (offset + 4 < endOffset) target[offset + 4] = (byte)((rawData & 0xff00000000) >> 32); |
| | 2 | 449 | | if (offset + 5 < endOffset) target[offset + 5] = (byte)((rawData & 0xff0000000000) >> 40); |
| | 2 | 450 | | if (offset + 6 < endOffset) target[offset + 6] = (byte)((rawData & 0xff000000000000) >> 48); |
| | 2 | 451 | | if (offset + 7 < endOffset) target[offset + 7] = (byte)((rawData & 0xff00000000000000) >> 56); |
| | | 452 | | } |
| | 2 | 453 | | } |
| | | 454 | | |
| | | 455 | | private const int DecimalMaxScalePlusOne = 29; |
| | | 456 | | /// <summary> |
| | | 457 | | /// Gets a <see cref="decimal"/> with a random value. All possible values should be roughly evenly distributed. |
| | | 458 | | /// </summary> |
| | 2 | 459 | | public decimal NextDecimal => new(NextInt32Signed, NextInt32Signed, NextInt32Signed, NextBoolean, (byte)(NextUInt32 |
| | | 460 | | /// <summary> |
| | | 461 | | /// Gets a <see cref="double"/> with a random value. All possible values should be roughly evenly distributed, incl |
| | | 462 | | /// </summary> |
| | 2 | 463 | | public double NextDouble => BitConverter.Int64BitsToDouble(NextInt64Signed); |
| | | 464 | | /// <summary> |
| | | 465 | | /// Gets a <see cref="Guid"/> with a random value. All possible values should be roughly evenly distributed. |
| | | 466 | | /// Note that these "GUID"s should *not* be used publicly where GUIDs are expected to have certain characteristics t |
| | | 467 | | /// These GUIDs should only be used for testing. |
| | | 468 | | /// </summary> |
| | | 469 | | public Guid NextGuid |
| | | 470 | | { |
| | | 471 | | get |
| | | 472 | | { |
| | 2 | 473 | | byte[] bytes = NextNewBytes(16); |
| | 2 | 474 | | return new Guid(bytes); |
| | | 475 | | } |
| | | 476 | | } |
| | | 477 | | /// <summary> |
| | | 478 | | /// Gets a random value for an enum of the specified type. All possible values should be roughly evenly distributed |
| | | 479 | | /// If the specified enum type is marked with <see cref="FlagsAttribute"/>, a random set of possible values are comb |
| | | 480 | | /// </summary> |
| | | 481 | | /// <param name="type">The type for the enum a random value is to be selected for. Must be a non-null enum type.</p |
| | | 482 | | /// <returns>A random value for that enum.</returns> |
| | | 483 | | public object NextEnum(Type type) |
| | | 484 | | { |
| | | 485 | | #if NET5_0_OR_GREATER |
| | 2 | 486 | | ArgumentNullException.ThrowIfNull(type); |
| | | 487 | | #else |
| | | 488 | | if (type is null) throw new ArgumentNullException(nameof(type)); |
| | | 489 | | #endif |
| | 2 | 490 | | if (!(typeof(System.Enum)).IsAssignableFrom(type)) throw new ArgumentException("The type must be an enum type!", |
| | 2 | 491 | | FieldInfo[] enumValues = type.GetFields(BindingFlags.Public | BindingFlags.Static); |
| | | 492 | | // is this a flags type? |
| | 2 | 493 | | if (type.GetCustomAttributes(typeof(FlagsAttribute), false).Length > 0) |
| | | 494 | | { |
| | | 495 | | // build a randomized sequential array |
| | 2 | 496 | | int[] enumValueSelectorIndex = new int[enumValues.Length]; |
| | 2 | 497 | | for (int value = 0; value < enumValues.Length; ++value) |
| | | 498 | | { |
| | 2 | 499 | | enumValueSelectorIndex[value] = value; |
| | | 500 | | } |
| | 2 | 501 | | for (int value = 0; value < enumValues.Length * 2; ++value) |
| | | 502 | | { |
| | 2 | 503 | | int pick1 = value % enumValues.Length; |
| | 2 | 504 | | int pick2 = NextInt32 % enumValues.Length; |
| | 2 | 505 | | int temp = enumValueSelectorIndex[pick1]; |
| | 2 | 506 | | enumValueSelectorIndex[pick1] = enumValueSelectorIndex[pick2]; |
| | 2 | 507 | | enumValueSelectorIndex[pick2] = temp; |
| | | 508 | | } |
| | | 509 | | // build a string containing random flag values |
| | 2 | 510 | | string enumStringValue = string.Empty; |
| | 2 | 511 | | int values = NextInt32 % enumValues.Length; |
| | | 512 | | // no values specified--use default value (0) |
| | 2 | 513 | | if (values == 0) |
| | | 514 | | { |
| | 2 | 515 | | return 0; |
| | | 516 | | } |
| | 2 | 517 | | for (int value = 0; value < values; ++value) |
| | | 518 | | { |
| | | 519 | | // Coverage note: the optimizer seems to insert a huge amount of code here with at least one branch, des |
| | 2 | 520 | | enumStringValue = "," + enumValues[enumValueSelectorIndex[value]].GetValue(type); |
| | | 521 | | } |
| | | 522 | | // have the CLR parse that value and give us a typed enum back |
| | | 523 | | #if NET6_0_OR_GREATER |
| | 2 | 524 | | return Enum.Parse(type, enumStringValue.AsSpan(1)); |
| | | 525 | | #else |
| | | 526 | | return Enum.Parse(type, enumStringValue.Substring(1)); |
| | | 527 | | #endif |
| | | 528 | | } |
| | 2 | 529 | | return enumValues[NextUInt32 % enumValues.Length].GetValue(type)!; // enum field values better not be null! |
| | | 530 | | } |
| | | 531 | | /// <summary> |
| | | 532 | | /// Gets a random typed enum value for a specific enum type. Values will be roughly evenly distributed across all p |
| | | 533 | | /// If the specified enum type is marked with <see cref="FlagsAttribute"/>, a random set of possible values are comb |
| | | 534 | | /// </summary> |
| | | 535 | | /// <typeparam name="TEnum">The type of enum to get a random value for.</typeparam> |
| | | 536 | | /// <returns>A random value for that enum.</returns> |
| | | 537 | | public TEnum NextEnum<TEnum>() |
| | | 538 | | { |
| | 2 | 539 | | return (TEnum)NextEnum(typeof(TEnum)); |
| | | 540 | | } |
| | | 541 | | /// <summary> |
| | | 542 | | /// Gets a 32-bit hash code for this instance. |
| | | 543 | | /// </summary> |
| | | 544 | | /// <returns>A 32-bit hash code for this instance.</returns> |
| | | 545 | | public override int GetHashCode() |
| | | 546 | | { |
| | 2 | 547 | | return _seed.GetHashCode(); |
| | | 548 | | } |
| | | 549 | | /// <summary> |
| | | 550 | | /// Checks to see if the specified object is logically equal to this one. |
| | | 551 | | /// </summary> |
| | | 552 | | /// <param name="obj">The object to compare to.</param> |
| | | 553 | | /// <returns>true if <paramref name="obj"/> is logically equal to this instance, false if it is not.</returns> |
| | | 554 | | public override bool Equals(object? obj) |
| | | 555 | | { |
| | 2 | 556 | | if (obj is not Pseudorandom) return false; |
| | 2 | 557 | | return Equals((Pseudorandom)obj); |
| | | 558 | | } |
| | | 559 | | /// <summary> |
| | | 560 | | /// Checks to see if the specified Pseudorandom is logically equal to this one. |
| | | 561 | | /// </summary> |
| | | 562 | | /// <param name="other">The Pseudorandom to compare to.</param> |
| | | 563 | | /// <returns>true if <paramref name="other"/> is logically equal to this instance, false if it is not.</returns> |
| | | 564 | | public bool Equals(Pseudorandom? other) |
| | | 565 | | { |
| | 2 | 566 | | if (other is null) return false; |
| | 2 | 567 | | return _seed.Equals(other._seed); |
| | | 568 | | } |
| | | 569 | | /// <summary> |
| | | 570 | | /// Checks to see if two Pseudorandoms are logically equal. |
| | | 571 | | /// </summary> |
| | | 572 | | /// <param name="a">The first Pseudorandom to compare.</param> |
| | | 573 | | /// <param name="b">The second Pseudorandom to compare.</param> |
| | | 574 | | /// <returns>true if the Pseudorandoms are the same, otherwise false.</returns> |
| | | 575 | | public static bool operator ==(Pseudorandom? a, Pseudorandom? b) |
| | | 576 | | { |
| | 2 | 577 | | if (ReferenceEquals(a, b)) return true; |
| | 2 | 578 | | if (a is null || b is null) return false; |
| | 2 | 579 | | return a._seed == b._seed; |
| | | 580 | | } |
| | | 581 | | /// <summary> |
| | | 582 | | /// Checks to see if two Pseudorandoms are logically not equal. |
| | | 583 | | /// </summary> |
| | | 584 | | /// <param name="a">The first Pseudorandom to compare.</param> |
| | | 585 | | /// <param name="b">The second Pseudorandom to compare.</param> |
| | | 586 | | /// <returns>true if the Pseudorandoms are logically not equal, otherwise false.</returns> |
| | | 587 | | public static bool operator !=(Pseudorandom? a, Pseudorandom? b) |
| | | 588 | | { |
| | 2 | 589 | | if (ReferenceEquals(a, b)) return false; |
| | 2 | 590 | | if (a is null || b is null) return true; |
| | 2 | 591 | | return a._seed != b._seed; |
| | | 592 | | } |
| | | 593 | | } |