コード例 #1
0
        public static long RandomLong(this RandomNumberGenerator random, long min, long max)
        {
            EnsureMinLEQMax(ref min, ref max);
            long numbersInRange = unchecked (max - min + 1);

            if (numbersInRange < 0)
            {
                throw new ArgumentException("Size of range between min and max must be less than or equal to Int64.MaxValue");
            }
            long randomOffset = random.RandomInt64();

            if (IsModuloBiased(randomOffset, numbersInRange))
            {
                return(RandomLong(random, min, max)); // Try again TODO recusion is evil
            }
            else
            {
                return(min + PositiveModuloOrZero(randomOffset, numbersInRange));
            }
        }