예제 #1
0
        // 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);
            }
        }
예제 #2
0
        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);
                    }
                }
            }
        }
예제 #3
0
        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;
            }
        }
예제 #4
0
        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);
            }
        }
예제 #5
0
 public static ulong NextU64()
 {
     lock (InnerOne) {
         return(InnerOneU64.Next());
     }
 }
예제 #6
0
 public static int Next()
 {
     lock (InnerOne) {
         return(InnerOne.Next());
     }
 }
예제 #7
0
 public TValue Next()
 {
     return(_values[_rng.Next(_count)]);
 }
예제 #8
0
 public static double NextDouble(this IRNG <ulong> rng) => rng.Next() * (1.0 / ulong.MaxValue);
예제 #9
0
 public static double NextDouble(this IRNG <int> rng) => unchecked ((uint)rng.Next()) * (1.0 / uint.MaxValue);
예제 #10
0
 public static int Next(this IRNG <int> rng, int max) => rng.Next(0, max);