Пример #1
0
        public void PseudorandomSerialDistributionInt32()
        {
            int[]        distribution = new int[short.MaxValue + 1];
            Pseudorandom rand         = Pseudorandom.Next;
            int          count        = 655360;

            for (int loop = 0; loop < count; ++loop)
            {
                ++distribution[rand.NextInt32 >> 16];
            }
            // compute the standard deviation
            double standardDeviation = StandardDeviation(distribution, distribution.Length, (double)distribution.Length / count);

            Assert.IsTrue(standardDeviation < 25.0);

            Assert.IsTrue(AverageFrequencyDecreaseFactor(distribution) < 2);

            distribution = new int[ushort.MaxValue + 1];
            rand         = Pseudorandom.Next;
            count        = 655360;
            for (int loop = 0; loop < count; ++loop)
            {
                ++distribution[(int)(rand.NextInt32 & 0xffff)];
            }
            // compute the standard deviation
            standardDeviation = StandardDeviation(distribution, distribution.Length, (double)distribution.Length / count);
            Assert.IsTrue(standardDeviation < 25.0);

            Assert.IsTrue(AverageFrequencyDecreaseFactor(distribution) < 2);
        }
Пример #2
0
 public void PseudorandomSeededRand()
 {
     for (int seed = 0; seed < 4892; seed += 17)
     {
         Pseudorandom rand  = (seed == 0) ? new Pseudorandom() : new Pseudorandom(seed - 1);
         Pseudorandom clone = rand.Clone();
         Assert.AreEqual(rand.NextInt32Signed, clone.NextInt32Signed);
         Assert.AreEqual(rand.NextInt64Signed, clone.NextInt64Signed);
         Assert.AreEqual(rand.NextBoolean, clone.NextBoolean);
         Assert.AreEqual(rand.NextDouble, clone.NextDouble);
         Assert.AreEqual(rand.NextGuid, clone.NextGuid);
         Assert.AreEqual(rand.NextInt32Signed, clone.NextInt32Signed);
         Assert.AreEqual(rand.NextInt64Signed, clone.NextInt64Signed);
         Assert.AreEqual(rand.NextSignMultiplier, clone.NextSignMultiplier);
         Assert.AreEqual(rand.NextUInt32, clone.NextUInt32);
         Assert.AreEqual(rand.NextUInt64, clone.NextUInt64);
         Assert.AreEqual(rand.NextInt32UsuallySmall, clone.NextInt32UsuallySmall);
         Assert.AreEqual(rand.NextInt64SignedUsuallySmall, clone.NextInt64SignedUsuallySmall);
         Assert.AreEqual(rand.GetHashCode(), clone.GetHashCode());
         Assert.AreEqual(rand.NextEnum(typeof(NormalEnum)), clone.NextEnum(typeof(NormalEnum)));
         Assert.AreEqual(rand.NextEnum <FlagsEnum>(), clone.NextEnum <FlagsEnum>());
         Assert.AreEqual(rand.NextInt32Ranged(54789), clone.NextInt32Ranged(54789));
         Assert.AreEqual(rand.NextInt32Ranged(54789, 285702), clone.NextInt32Ranged(54789, 285702));
         Assert.AreEqual(rand.NextInt64Ranged(8902432), clone.NextInt64Ranged(8902432));
         Assert.AreEqual(rand.NextInt64Ranged(8902432, 5427890480323L), clone.NextInt64Ranged(8902432, 5427890480323L));
         Assert.IsTrue(ArrayExtensions.ValueEquals(rand.NextNewBytes(5), clone.NextNewBytes(5)));
         Assert.AreEqual(rand.NextInt32RangedUsuallySmall(2), clone.NextInt32RangedUsuallySmall(2));
         Assert.AreEqual(rand.NextInt32RangedUsuallySmall(90432, 2), clone.NextInt32RangedUsuallySmall(90432, 2));
         Assert.AreEqual(rand.NextInt64RangedUsuallySmall(2), clone.NextInt64RangedUsuallySmall(2));
         Assert.AreEqual(rand.NextInt64RangedUsuallySmall(90464, 2), clone.NextInt64RangedUsuallySmall(90464, 2));
         Assert.AreEqual(rand.ToString(), clone.ToString());
     }
 }
Пример #3
0
        public void PseudorandomExceptions()
        {
            Pseudorandom rand = new Pseudorandom(false);

            Assert.ThrowsException <ArgumentNullException>(() => rand.NextEnum(null !));
            Assert.ThrowsException <ArgumentNullException>(() => rand.NextBytes(null !, 1));
            Assert.ThrowsException <ArgumentException>(() => rand.NextInt32SignedRangedUsuallySmall(10, 5));
            Assert.ThrowsException <ArgumentException>(() => rand.NextInt64SignedRangedUsuallySmall(10, 5));
            Assert.ThrowsException <ArgumentException>(() => rand.NextEnum(typeof(int)));
        }
Пример #4
0
        public void PseudorandomUsuallyVerySmall()
        {
            int          loops = 1000;
            Pseudorandom rand;

            rand = new Pseudorandom(4387617);
            for (int loop = 0; loop < loops; ++loop)
            {
                uint  ui = rand.NextUInt32RangedUsuallySmall(uint.MaxValue, 37);
                ulong ul = rand.NextUInt64RangedUsuallySmall(uint.MaxValue, 67);
            }
        }
Пример #5
0
        public void PseudorandomSigned()
        {
            int          loops = 10000;
            double       regularAbsSum;
            double       regularSum;
            double       smallAbsSum;
            double       smallSum;
            Pseudorandom rand;

            regularAbsSum = 0;
            regularSum    = 0;
            smallAbsSum   = 0;
            smallSum      = 0;
            rand          = new Pseudorandom(4387617);
            for (int loop = 0; loop < loops; ++loop)
            {
                int normal = rand.NextInt32Signed;
                regularAbsSum += Math.Abs(normal);
                regularSum    += normal;
                int small = rand.NextInt32SignedUsuallySmall;
                smallAbsSum += Math.Abs(small);
                smallSum    += small;
            }
            Assert.IsTrue(regularAbsSum / loops > int.MaxValue / 4);
            Assert.IsTrue(Math.Abs(regularSum) / loops / int.MaxValue < .01);
            Assert.IsTrue(Math.Abs(regularSum / regularAbsSum) < .01);
            Assert.IsTrue(Math.Abs(smallAbsSum / regularAbsSum) < .1);
            Assert.IsTrue(smallAbsSum / loops > int.MaxValue / 2_000);
            Assert.IsTrue(Math.Abs(smallSum) / loops < int.MaxValue / 2_000);
            Assert.IsTrue(Math.Abs(smallSum / smallAbsSum) < .2);

            regularAbsSum = 0;
            regularSum    = 0;
            smallAbsSum   = 0;
            smallSum      = 0;
            rand          = new Pseudorandom(4387617);
            for (int loop = 0; loop < loops; ++loop)
            {
                long normal = rand.NextInt64Signed;
                regularAbsSum += Math.Abs(normal);
                regularSum    += normal;
                long small = rand.NextInt64SignedUsuallySmall;
                smallAbsSum += Math.Abs(small);
                smallSum    += small;
            }
            Assert.IsTrue(regularAbsSum / loops > long.MaxValue / 4);
            Assert.IsTrue(Math.Abs(regularSum) / loops / long.MaxValue < .01);
            Assert.IsTrue(Math.Abs(regularSum / regularAbsSum) < .01);
            Assert.IsTrue(Math.Abs(smallAbsSum / regularAbsSum) < .1);
            Assert.IsTrue(smallAbsSum / loops > long.MaxValue / 5_000_000);
            Assert.IsTrue(Math.Abs(smallSum) / loops < long.MaxValue / 5_000_000);
            Assert.IsTrue(Math.Abs(smallSum / smallAbsSum) < .5);
        }
Пример #6
0
        public void PseudorandomSignedAlwaysPositive()
        {
            int          loops = 10000;
            Pseudorandom rand;

            rand = new Pseudorandom(4387617);
            for (int loop = 0; loop < loops; ++loop)
            {
                int i = rand.NextInt32SignedRangedUsuallySmall(1, int.MaxValue);
                Assert.IsTrue(i > 0);
                long l = rand.NextInt64SignedRangedUsuallySmall(1, long.MaxValue);
                Assert.IsTrue(l > 0);
            }
        }
Пример #7
0
        public void PseudorandomSignedAlwaysNegative()
        {
            int          loops = 10000;
            Pseudorandom rand;

            rand = new Pseudorandom(4387617);
            for (int loop = 0; loop < loops; ++loop)
            {
                int i = rand.NextInt32SignedRangedUsuallySmall(int.MinValue, -1);
                Assert.IsTrue(i < 0);
                long l = rand.NextInt64SignedRangedUsuallySmall(long.MinValue, -1);
                Assert.IsTrue(l < 0);
            }
        }
Пример #8
0
        public void PseudorandomSeeds()
        {
            Pseudorandom rand    = new Pseudorandom(true);
            int          matches = 0;

            for (int attempt = 0; attempt < 10; ++attempt)
            {
                matches += (new Pseudorandom(true) == rand) ? 1 : 0;
                System.Threading.Thread.Sleep(15);
            }
            Assert.IsTrue(matches < 2); // there is some randomness due to the time factor integrated into the global seed generator, which could cause the seeds to be the same in rare cases

            Assert.AreNotEqual(new Pseudorandom(false), rand);
        }
Пример #9
0
        public void PseudorandomEquality()
        {
            Pseudorandom rand = new Pseudorandom(5);

            Assert.AreEqual(new Pseudorandom(5).GetHashCode(), rand.GetHashCode());
            Assert.IsFalse(rand.Equals(this));
            Assert.IsFalse(rand.Equals(null));
            Pseudorandom clone = rand !.Clone(); // not sure how the analyzer gets confused here?  maybe it's seeing the Equals(null) above and getting confused about that

            Assert.IsTrue(rand.Equals(clone));
            Assert.IsTrue(rand.Equals((object)clone));
            Pseudorandom rand2 = new Pseudorandom(6);

            Assert.IsFalse(rand == rand2);
            Assert.IsTrue(rand != rand2);
        }
Пример #10
0
        public void PseudorandomRanged()
        {
            Pseudorandom rand = new Pseudorandom(4387617);

            Assert.ThrowsException <ArgumentException>(() => rand.NextInt32Ranged(10, 9));
            Assert.ThrowsException <ArgumentException>(() => rand.NextInt32SignedRanged(0, -100));
            Assert.ThrowsException <ArgumentException>(() => rand.NextUInt32Ranged(10, 9));
            Assert.ThrowsException <ArgumentException>(() => rand.NextInt64Ranged(10, 9));
            Assert.ThrowsException <ArgumentException>(() => rand.NextInt64SignedRanged(0, -100));
            Assert.ThrowsException <ArgumentException>(() => rand.NextUInt64Ranged(10, 9));
            Assert.AreEqual(9, rand.NextInt32Ranged(9, 9));
            Assert.AreEqual(-1, rand.NextInt32SignedRanged(-1, -1));
            Assert.AreEqual(9U, rand.NextUInt32Ranged(9, 9));
            Assert.AreEqual(9L, rand.NextInt64Ranged(9, 9));
            Assert.AreEqual(-1L, rand.NextInt64SignedRanged(-1, -1));
            Assert.AreEqual(9UL, rand.NextUInt64Ranged(9, 9));
        }
Пример #11
0
        public void PseudorandomSerialUsuallySmallDistributionInt64Ranged()
        {
            int[]        distribution = new int[ushort.MaxValue + 1];
            Pseudorandom rand         = new Pseudorandom(4387617);
            int          count        = 655360;

            for (int loop = 0; loop < count; ++loop)
            {
                ++distribution[rand.NextInt64UsuallySmall >> 48];
            }
            // compute the standard deviation
            double standardDeviation = StandardDeviation(distribution, distribution.Length, (double)distribution.Length / count);

            Assert.IsTrue(standardDeviation > 200.0);

            Assert.IsTrue(AverageFrequencyDecreaseFactor(distribution) > 1000);
        }
Пример #12
0
        public void PseudorandomNullReferences()
        {
            Pseudorandom a = new Pseudorandom(0);
            Pseudorandom b = new Pseudorandom(0);
            Pseudorandom c = new Pseudorandom(1);
            Pseudorandom n = null !;

            Assert.IsTrue(a == b);
            Assert.IsFalse(a == c);
            Assert.IsFalse(a == n);
            Assert.IsFalse(n == a);
            Assert.IsTrue(n == null);
            Assert.IsFalse(a != b);
            Assert.IsTrue(a != c);
            Assert.IsFalse(n != null);
            Assert.IsTrue(a != n);
            Assert.IsTrue(n != a);
        }
Пример #13
0
 public double Sample() => Pseudorandom.NextDouble();