Beispiel #1
0
        public static int NextInt(UniformRandomProvider generator, int bound)
        {
            if (bound <= 0)
            {
                throw new ArgumentException("bound must be strictly positive");
            }

            uint r = ((uint)generator.NextInt()) >> 1;
            int  m = bound - 1;

            if ((bound & m) == 0)  // i.e., bound is a power of 2
            {
                r = (uint)((bound * (long)r) >> 31);
            }
            else
            {
                // reject over-represented candidates
                uint u = r;
                while (u - (r = u % (uint)bound) + m < 0)
                {
                    u = ((uint)generator.NextInt()) >> 1;
                }
            }
            return((int)r);
        }
Beispiel #2
0
        public static long NextLong(UniformRandomProvider generator, long bound)
        {
            if (bound <= 0)
            {
                throw new ArgumentException("bound must be strictly positive");
            }

            long r = generator.NextLong();
            long m = bound - 1;

            if ((bound & m) == 0)
            {  // i.e., bound is a power of 2
                r = r & m;
            }
            else
            {
                // reject over-represented candidates
                long u = r >> 1;
                while (u + m - (r = u % bound) < 0L)
                {
                    u = generator.NextLong() >> 1;
                }
            }
            return(r);
        }
Beispiel #3
0
 public static int NextInt(UniformRandomProvider generator, int origin, int bound)
 {
     if (origin < bound)
     {
         int n = bound - origin;
         if (n > 0)
         {
             return(generator.NextInt(n) + origin);
         }
         else
         {  // range not representable as int
             int r;
             do
             {
                 r = generator.NextInt();
             } while (r < origin || r >= bound);
             return(r);
         }
     }
     else
     {
         return(generator.NextInt());
     }
 }
Beispiel #4
0
 public static IEnumerable <bool> NextBooleans(this UniformRandomProvider generator)
 => Generate(generator.NextBoolean);
Beispiel #5
0
 public static IEnumerable <bool> NextBooleans(this UniformRandomProvider generator, long streamSize)
 => NextBooleans(generator).TakeLong(streamSize);
Beispiel #6
0
 public static IEnumerable <float> NextFloats(this UniformRandomProvider generator, long streamSize)
 => NextFloats(generator).TakeLong(streamSize);
Beispiel #7
0
 public static IEnumerable <float> NextFloats(this UniformRandomProvider generator)
 => Generate(generator.NextFloat);
Beispiel #8
0
 public static IEnumerable <long> NextLongs(this UniformRandomProvider generator, long streamSize, int origin, int bound)
 => NextLongs(generator, origin, bound).TakeLong(streamSize);
Beispiel #9
0
 public static IEnumerable <long> NextLongs(this UniformRandomProvider generator, int origin, int bound)
 => Generate(() => generator.NextLong(bound - origin) + origin);
Beispiel #10
0
 public static IEnumerable <long> NextLongs(this UniformRandomProvider generator)
 => Generate(generator.NextLong);