Пример #1
0
        public void Bytes()
        {
            var    rng    = new StepRng(0);
            Random random = RandomShim.Create(rng);

            var buffer = new Byte[1024];

            random.NextBytes(buffer);
            var ints = MemoryMarshal.Cast <Byte, UInt64>(buffer);

            for (Int32 i = 0; i < ints.Length; i++)
            {
                Assert.Equal((UInt64)i, ints[i]);
            }

#if !NET472
            random.NextBytes(buffer.AsSpan());
            for (Int32 i = 0; i < ints.Length; i++)
            {
                Assert.Equal((UInt64)(i + ints.Length), ints[i]);
            }
#endif

            var maxRng = new StepRng(UInt64.MaxValue)
            {
                Increment = 0
            };
            var maxShim = RandomNumberGeneratorShim.Create(maxRng);

            buffer = new Byte[1024];
            maxShim.GetBytes(buffer, 200, 500);
            Assert.All(buffer.Take(200), b => Assert.Equal(0, b));
            Assert.All(buffer.Skip(200).Take(500), b => Assert.Equal(Byte.MaxValue, b));
            Assert.All(buffer.Skip(700), b => Assert.Equal(0, b));
        }
Пример #2
0
        public void RejectionsInt64()
        {
            const Int64  midpoint     = Int64.MaxValue / 2 + Int64.MinValue / 2;
            const Int64  low          = Int64.MinValue;
            const Int64  high         = midpoint + 1;
            const UInt64 maxRand      = sizeof(Int64) > 4 ? UInt64.MaxValue : UInt32.MaxValue;
            const UInt64 rangeSize    = unchecked ((UInt64)high - (UInt64)low + 1);
            const UInt64 rejectCount  = (maxRand - rangeSize + 1) % rangeSize;
            const UInt64 lastAccepted = maxRand - rejectCount;

            var dist = Uniform.NewInclusive(low, high);
            var rng  = new StepRng(lastAccepted - 1);

            Assert.True(dist.TrySample(rng, out Int64 result));
            Assert.Equal(midpoint, result);
            Assert.True(dist.TrySample(rng, out result));
            Assert.Equal(midpoint + 1, result);
            Assert.False(dist.TrySample(rng, out _));
            Assert.False(dist.TrySample(rng, out _));
            Assert.False(dist.TrySample(rng, out _));

            // Now test a blocking sample
            rng.State = maxRand - Math.Min(20, rejectCount) + 1;
            Assert.Equal(Int64.MinValue, dist.Sample(rng));
        }
Пример #3
0
        public void Rejections()
        {
            const Int64  midpoint     = Int64.MaxValue / 2 + Int64.MinValue / 2;
            const Int64  lowInt       = Int64.MinValue;
            const Int64  highInt      = midpoint + 1;
            const UInt64 maxRand      = UInt64.MaxValue;
            const UInt64 rangeSize    = unchecked (highInt - (UInt64)lowInt + 1);
            const UInt64 rejectCount  = (maxRand - rangeSize + 1) % rangeSize;
            const UInt64 lastAccepted = maxRand - rejectCount;

            var low  = TimeSpan.FromTicks(lowInt);
            var high = TimeSpan.FromTicks(highInt);
            var dist = Uniform.NewInclusive(low, high);
            var rng  = new StepRng(lastAccepted - 1);

            Assert.True(dist.TrySample(rng, out TimeSpan result));
            Assert.Equal(midpoint, result.Ticks);
            Assert.True(dist.TrySample(rng, out result));
            Assert.Equal(midpoint + 1, result.Ticks);
            Assert.False(dist.TrySample(rng, out _));
            Assert.False(dist.TrySample(rng, out _));
            Assert.False(dist.TrySample(rng, out _));

            // Now test a blocking sample
            rng.State = maxRand - Math.Min(20, rejectCount) + 1;
            Assert.Equal(TimeSpan.MinValue, dist.Sample(rng));
        }
Пример #4
0
        public void Next()
        {
            var    rng    = new StepRng(0);
            Random random = RandomShim.Create(rng);

            for (Int32 i = 0; i < 100; i++)
            {
                Assert.Equal(i, random.Next());
            }

            for (Int32 i = 0; i < 100; i++)
            {
                Assert.Equal(i, random.Next(100));
            }

            for (Int32 i = 0; i < 100; i++)
            {
                Assert.Equal(i, random.Next(0, 100));
            }

            for (Int32 i = 0; i < 10_000; i++)
            {
                var value = random.NextDouble();
                Assert.True(0 <= value && value < 1);
            }
        }
Пример #5
0
 public void DecimalRanges()
 {
     var zeroRng = new StepRng(0)
     {
         Increment = 0
     };                                                                                                  // Generates 0.0
     var     openZeroRng = new SequenceRng(new UInt32[] { 1, 0, 0 });                                    // Generates the lowest non-zero value
     var     openOneRng = new SequenceRng(new UInt32[] { 0x1000_0000, 0x3E25_0261, 0x204F_CE5Du << 2 }); // Generates the value just under 1.0
Пример #6
0
 public Extensions()
 {
     _rng  = new StepRng(0);
     _list = new List <Int32>
     {
         1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
         11, 12, 13, 14, 15, 16, 17, 18, 19, 20
     };
 }
Пример #7
0
        public void UnlikelyFalse()
        {
            // This distribution normally has a probably of success of 2^64 - 1 / 2^64,
            // so this is very unlikely to return false.
            var dist = Bernoulli.FromInverse(UInt64.MaxValue);
            // BUt we'll force it to anyway.
            var rng = new StepRng(UInt64.MaxValue);

            Assert.False(dist.Sample(rng));
        }
Пример #8
0
        public void FillViaNextCleanly()
        {
            var rng    = new StepRng(0);
            var buffer = new UInt64[8];

            Filler.FillBytesViaNext(rng, MemoryMarshal.Cast <UInt64, Byte>(buffer));
            for (UInt32 i = 0; i < buffer.Length; i++)
            {
                Assert.Equal(i, buffer[i]);
            }
        }
Пример #9
0
        public void BadArgs()
        {
            var rng   = new StepRng(0);
            var shim  = RandomNumberGeneratorShim.Create(rng);
            var bytes = new Byte[32];

            Assert.Throws <ArgumentNullException>(() => shim.GetBytes(null !, 0, 10));
            Assert.Throws <ArgumentOutOfRangeException>(() => shim.GetBytes(bytes, -10, 10));
            Assert.Throws <ArgumentOutOfRangeException>(() => shim.GetBytes(bytes, 10, -10));
            Assert.Throws <ArgumentException>(() => shim.GetBytes(bytes, 32, 1));
        }
Пример #10
0
        public void FillViaNext4BytesLeft()
        {
            var rng    = new StepRng(0);
            var buffer = new UInt32[17];

            Filler.FillBytesViaNext(rng, MemoryMarshal.Cast <UInt32, Byte>(buffer));
            for (UInt32 i = 0; i < buffer.Length / 2; i += 2)
            {
                Assert.Equal(i / 2, buffer[i]);
                Assert.Equal(0u, buffer[i + 1]);
            }
            Assert.Equal(8u, buffer[16]);
        }
Пример #11
0
        public void FillViaNext2BytesLeft()
        {
            var rng    = new StepRng(0);
            var buffer = new UInt16[33];

            Filler.FillBytesViaNext(rng, MemoryMarshal.Cast <UInt16, Byte>(buffer));
            for (UInt32 i = 0; i < buffer.Length / 4; i += 4)
            {
                Assert.Equal(i / 4, buffer[i]);
                Assert.Equal(0u, buffer[i + 1]);
                Assert.Equal(0u, buffer[i + 2]);
                Assert.Equal(0u, buffer[i + 3]);
            }
            Assert.Equal(8u, buffer[32]);
        }
Пример #12
0
        public void DoubleRange(Double low, Double high)
        {
            var rng     = Pcg32.Create(252, 11634580027462260723ul);
            var zeroRng = new StepRng(0)
            {
                Increment = 0
            };
            var maxRng = new StepRng(0xFFFF_FFFF_FFFF_FFFF)
            {
                Increment = 0
            };

            var uniform          = Uniform.New(low, high);
            var inclusiveUniform = Uniform.NewInclusive(low, high);

            for (var i = 0; i < 100; i++)
            {
                var exclusive = uniform.Sample(rng);
                Assert.True(low <= exclusive && exclusive < high, $"Exclusive sampling of RNG failed; low: {low}, high: {high}, actual: {exclusive}");
                var inclusive = uniform.Sample(rng);
                Assert.True(low <= inclusive && inclusive <= high, $"Inclusive sampling of RNG failed; low: {low}, high: {high}, actual: {inclusive}");
            }

            Assert.Equal(low, Uniform.NewInclusive(low, low).Sample(rng));

            Assert.Equal(low, uniform.Sample(zeroRng));
            Assert.Equal(low, inclusiveUniform.Sample(zeroRng));
            Assert.True(uniform.Sample(maxRng) < high, $"Exclusive sampling of Max RNG failed; low: {low}, high: {high}, actual: {uniform.Sample(maxRng)}");
            Assert.True(inclusiveUniform.Sample(maxRng) <= high, $"Inclusive sampling of Max RNG failed; low: {low}, high: {high}, actual: {inclusiveUniform.Sample(maxRng):G19}");

            // Don't run this test for really tiny differences between high and low
            // since for those rounding might result in selecting high for a very
            // long time.
            if (high - low <= 0.0001)
            {
                return;
            }

            var loweringMaxRng = new StepRng(0xFFFF_FFFF_FFFF_FFFF)
            {
                Increment = unchecked ((UInt64)(-1L << 12))
            };

            Assert.True(uniform.Sample(loweringMaxRng) < high);
            Assert.True(uniform.Sample(loweringMaxRng) < high);
        }
Пример #13
0
        public void FloatTest(Decimal low, Decimal high)
        {
            var rng     = Pcg32.Create(252, 11634580027462260723ul);
            var zeroRng = new StepRng(0)
            {
                Increment = 0
            };
            var maxRng = new StepRng(0xFFFF_FFFF_FFFF_FFFF)
            {
                Increment = 0
            };

            var exclusiveUniform = Uniform.New(low, high);
            var inclusiveUniform = Uniform.NewInclusive(low, high);

            for (var i = 0; i < 100; i++)
            {
                var exclusive = exclusiveUniform.Sample(rng);
                Assert.True(low <= exclusive && exclusive < high);
                var inclusive = exclusiveUniform.Sample(rng);
                Assert.True(low <= inclusive && inclusive <= high);
            }

            Assert.Equal(low, Uniform.NewInclusive(low, low).Sample(rng));

            Assert.Equal(low, exclusiveUniform.Sample(zeroRng));
            Assert.Equal(low, inclusiveUniform.Sample(zeroRng));
            Assert.True(exclusiveUniform.Sample(maxRng) < high);
            Assert.True(inclusiveUniform.Sample(maxRng) <= high);

            var loweringMaxRng = new StepRng(0xFFFF_FFFF_FFFF_FFFF)
            {
                Increment = unchecked ((UInt64)(-1L << 12))
            };

            Assert.True(exclusiveUniform.Sample(loweringMaxRng) < high);
            Assert.True(exclusiveUniform.Sample(loweringMaxRng) < high);

            var maxDoubleInclusive = Uniform.NewInclusive(Decimal.MaxValue, Decimal.MaxValue);

            Assert.Equal(Decimal.MaxValue, maxDoubleInclusive.Sample(rng));
            var minDoubleInclusive = Uniform.NewInclusive(-Decimal.MaxValue, -Decimal.MaxValue);

            Assert.Equal(-Decimal.MaxValue, minDoubleInclusive.Sample(rng));
        }
Пример #14
0
        public UniformDists()
        {
            _rng          = new StepRng(0);
            _uniformSByte = Uniform.New((SByte)LowerBound, (SByte)UpperBound);
            _uniformInt16 = Uniform.New((Int16)LowerBound, (Int16)UpperBound);
            _uniformInt32 = Uniform.New(LowerBound, UpperBound);
            _uniformInt64 = Uniform.New((Int64)LowerBound, (Int64)UpperBound);

            _uniformByte   = Uniform.New((Byte)LowerBound, (Byte)UpperBound);
            _uniformUInt16 = Uniform.New((UInt16)LowerBound, (UInt16)UpperBound);
            _uniformUInt32 = Uniform.New((UInt32)LowerBound, (UInt32)UpperBound);
            _uniformUInt64 = Uniform.New((UInt64)LowerBound, (UInt64)UpperBound);

            _uniformSingle = Uniform.New((Single)LowerBound, (Single)UpperBound);
            _uniformDouble = Uniform.New((Double)LowerBound, (Double)UpperBound);

            _uniformTimeSpan = Uniform.New(TimeSpan.FromHours(LowerBound), TimeSpan.FromHours(UpperBound));
        }
Пример #15
0
        public void DoubleRanges()
        {
            var zeroRng = new StepRng(0)
            {
                Increment = 0
            };
            var maxRng = new StepRng(UInt64.MaxValue)
            {
                Increment = 0
            };
            Double low, high;

            var closedOpen = ClosedOpen.Double.Instance;

            low  = closedOpen.Sample(zeroRng);
            high = closedOpen.Sample(maxRng);

            Assert.Equal(0, low);
            Assert.True(0 < high && high < 1);

            var openClosed = OpenClosed.Double.Instance;

            low  = openClosed.Sample(zeroRng);
            high = openClosed.Sample(maxRng);

            Assert.True(0 < low && low < 1);
            Assert.Equal(1, high);

            var closed = Closed.Double.Instance;

            low  = closed.Sample(zeroRng);
            high = closed.Sample(maxRng);

            Assert.Equal(0, low);
            Assert.Equal(1, high);

            var open = Open.Double.Instance;

            low  = open.Sample(zeroRng);
            high = open.Sample(maxRng);

            Assert.True(0 < low && low < 1);
            Assert.True(0 < high && high < 1);
        }
Пример #16
0
        public void FullRange()
        {
            var rng  = new StepRng(UInt64.MaxValue - 4);
            var dist = Uniform.NewInclusive(TimeSpan.MinValue, TimeSpan.MaxValue);

            _ = dist.Sample(rng); // Sample shouldn't need to retry
            // Mix up Sample and TrySample for the fun of it
            Assert.Equal(UInt64.MaxValue - 3, rng.State);
            Assert.True(dist.TrySample(rng, out _));
            _ = dist.Sample(rng);
            Assert.True(dist.TrySample(rng, out _));
            Assert.Equal(UInt64.MaxValue, rng.State);

            // The full range is a special case, where the distribution doesn't need to add _low,
            // so it simply casts directly to the result. The upshot of which is that signed and
            // unsigned distributions will behave differently, so we have to do bitwise comparisons
            // instead of using type.MaxValue and type.MinValue.
            Assert.Equal(TimeSpan.FromTicks(-1), dist.Sample(rng)); // 0
            Assert.True(dist.TrySample(rng, out TimeSpan result));  // RNG wraps around to 0
            Assert.Equal(TimeSpan.Zero, result);
        }
Пример #17
0
        public void NextUInt32ViaUInt64(UInt64 initialRngState, UInt32 expected)
        {
            var rng = new StepRng(initialRngState);

            Assert.Equal(expected, Filler.NextUInt32ViaUInt64(rng));
        }