// 4x faster than old one public static void Fill(this IRNG <int> rng, byte[] buffer, int start, int count) { if (rng == null) { throw new ArgumentNullException(nameof(rng)); } if (buffer == null) { throw new ArgumentNullException(nameof(buffer)); } var len = buffer.Length; if (start < 0 || start >= len) { throw new ArgumentOutOfRangeException(nameof(start)); } if (count < 0 || start + count > len) { throw new ArgumentOutOfRangeException(nameof(count)); } int end = start + count; int ptr = start; while ((ptr & 0x03) != 0 && ptr < end) { buffer[ptr++] = (byte)(rng.Next() & 0xFF); } unsafe { fixed(byte *bptr = buffer) { while ((ptr + 4) <= end) { *(int *)(bptr + ptr) = rng.Next(); ptr += 4; } } } while (ptr < end) { buffer[ptr++] = (byte)(rng.Next() & 0xFF); } }
public void Roll() { if (CanRoll()) { RollCount++; bool[] diceToHold = new bool[] { HoldDie1, HoldDie2, HoldDie3, HoldDie4, HoldDie5 }; for (int i = 0; i < dice.Count; i++) { if (!diceToHold[i]) { dice[i] = numberGenerator.Next(1, 7); } } } }
public static void ShuffleBy <T>(this T[] array, IRNG <int> rng) { int l = array.Length; for (int i = 0; i < l - 1; ++i) { var j = rng.Next(i, l); if (i == j) { continue; } var tmp = array[i]; array[i] = array[j]; array[j] = tmp; } }
public static ulong Next(this IRNG <ulong> rng, ulong max) { if (max <= 0) { throw new ArgumentOutOfRangeException(nameof(max)); } unchecked { var threshold = 0 - (0 - max) % max; uint rn; do { rn = (uint)rng.Next(); } while (rn > threshold); return(rn % max); } }
public static ulong NextU64() { lock (InnerOne) { return(InnerOneU64.Next()); } }
public static int Next() { lock (InnerOne) { return(InnerOne.Next()); } }
public TValue Next() { return(_values[_rng.Next(_count)]); }
public static double NextDouble(this IRNG <ulong> rng) => rng.Next() * (1.0 / ulong.MaxValue);
public static double NextDouble(this IRNG <int> rng) => unchecked ((uint)rng.Next()) * (1.0 / uint.MaxValue);
public static int Next(this IRNG <int> rng, int max) => rng.Next(0, max);