Ejemplo n.º 1
0
        public void Roll_ReturnValueBasedOnProbabilityDistribution()
        {
            // Setup
            var spec = new[]
            {
                new ValueProbabilityPair(0, 0.5),
                new ValueProbabilityPair(1, 0.5),
            };
            var distribution = new DiscreteValueProbabilityDistribution(spec);

            var expectedValueAndProbabilities = new[]
            {
                Tuple.Create(0, 0.0),
                Tuple.Create(0, 0.324),
                Tuple.Create(0, 0.5),
                Tuple.Create(1, 0.5 + 1e-6),
                Tuple.Create(1, 0.75),
                Tuple.Create(1, 1.0),
            };

            var rng = new TestingRandomNumberGenerator();

            rng.AddFactorValues(expectedValueAndProbabilities.Select(t => t.Item2));

            AbstractDie die = new TestAbstractDie(rng, distribution);

            foreach (Tuple <int, double> tuple in expectedValueAndProbabilities)
            {
                // Call
                int rollResult = die.Roll();

                // Assert
                Assert.AreEqual(tuple.Item1, rollResult);
            }
        }
Ejemplo n.º 2
0
        public void Roll_BasedOnProbabilityDistribution_ReturnExpectedResult(ComparisonType comparisonType)
        {
            // Setup
            var spec = new[]
            {
                new ValueProbabilityPair(1, 0.25),
                new ValueProbabilityPair(4, 0.5),
                new ValueProbabilityPair(8, 0.25),
            };
            var die = MockRepository.GenerateStub <IAbstractDie>();

            die.Stub(d => d.ProbabilityDistribution).Return(new DiscreteValueProbabilityDistribution(spec));

            var rng = new TestingRandomNumberGenerator();

            const int referenceValue   = 4;
            var       thresholdCompare = new ThresholdCompare(die, comparisonType, referenceValue, rng);

            double successProbability            = thresholdCompare.ProbabilityDistribution.SuccessProbability;
            var    expectedValueAndProbabilities = new[]
            {
                Tuple.Create(true, 0.0),
                Tuple.Create(true, successProbability - 0.1),
                Tuple.Create(true, successProbability),
                Tuple.Create(false, successProbability + 1e-6),
                Tuple.Create(false, successProbability + 0.1),
                Tuple.Create(false, 1.0),
            };

            rng.AddFactorValues(expectedValueAndProbabilities.Select(t => t.Item2));

            foreach (Tuple <bool, double> expectedValueAndProbability in expectedValueAndProbabilities)
            {
                // Call
                bool rolledResult = thresholdCompare.Roll();

                // Assert
                Assert.AreEqual(rolledResult, expectedValueAndProbability.Item1);
            }
        }
Ejemplo n.º 3
0
        public void Roll_ReturnRandomlyGeneratedValuesAccordingToProbabilityDistribution()
        {
            // Setup
            const double uniformProbability         = 1.0 / 6.0;
            var          rngValueAndExpectedResults = new[]
            {
                Tuple.Create(0.0, 1),
                Tuple.Create(0.45 * uniformProbability, 1),
                Tuple.Create(uniformProbability, 1),
                Tuple.Create(uniformProbability + 1e-6, 2),
                Tuple.Create(1.34 * uniformProbability, 2),
                Tuple.Create(2 * uniformProbability, 2),
                Tuple.Create((2 * uniformProbability) + 1e-6, 3),
                Tuple.Create(2.89 * uniformProbability, 3),
                Tuple.Create(3 * uniformProbability, 3),
                Tuple.Create((3 * uniformProbability) + 1e-6, 4),
                Tuple.Create(3.75 * uniformProbability, 4),
                Tuple.Create(4 * uniformProbability, 4),
                Tuple.Create((4 * uniformProbability) + 1e-6, 5),
                Tuple.Create(4.28 * uniformProbability, 5),
                Tuple.Create(5 * uniformProbability, 5),
                Tuple.Create((5 * uniformProbability) + 1e-6, 6),
                Tuple.Create(5.83 * uniformProbability, 6),
                Tuple.Create(6 * uniformProbability, 6),
            };
            var rng = new TestingRandomNumberGenerator();

            rng.AddFactorValues(rngValueAndExpectedResults.Select(t => t.Item1));

            var die = new NumericalDie(6, rng);

            foreach (Tuple <double, int> pair in rngValueAndExpectedResults)
            {
                // Call
                int dieResult = die.Roll();

                // Assert
                Assert.AreEqual(pair.Item2, dieResult, $"Asserting for probability value {pair.Item1}.");
            }
        }
Ejemplo n.º 4
0
        public void Roll_ReturnRamdomlyGeneratedValueAccordingToProbabilityDistribution()
        {
            // Setup
            var rngExpectedValueAndProbability = new[]
            {
                Tuple.Create(2, 0.0),
                Tuple.Create(2, 0.68 / 36.0),
                Tuple.Create(2, 1.0 / 36.0),
                Tuple.Create(3, (1.0 + 1e-6) / 36.0),
                Tuple.Create(3, 1.234 / 36.0),
                Tuple.Create(3, 3.0 / 36.0),
                Tuple.Create(4, (3.0 + 1e-6) / 36.0),
                Tuple.Create(4, 4.768 / 36.0),
                Tuple.Create(4, 6.0 / 36.0),
                Tuple.Create(5, (6.0 + 1e-6) / 36.0),
                Tuple.Create(5, 8.96 / 36.0),
                Tuple.Create(5, 10.0 / 36.0),
                Tuple.Create(6, (10.0 + 1e-6) / 36.0),
                Tuple.Create(6, 12.5678 / 36.0),
                Tuple.Create(6, 15.0 / 36.0),
                Tuple.Create(7, (15.0 + 1e-6) / 36.0),
                Tuple.Create(7, 19.856 / 36.0),
                Tuple.Create(7, 21.0 / 36.0),
                Tuple.Create(8, (21.0 + 1e-6) / 36.0),
                Tuple.Create(8, 23.68 / 36.0),
                Tuple.Create(8, 26.0 / 36.0),
                Tuple.Create(9, (26.0 + 1e-6) / 36.0),
                Tuple.Create(9, 29.56 / 36.0),
                Tuple.Create(9, 30.0 / 36.0),
                Tuple.Create(10, (30.0 + 1e-6) / 36.0),
                Tuple.Create(10, 31.34 / 36.0),
                Tuple.Create(10, 33.0 / 36.0),
                Tuple.Create(11, (33.0 + 1e-6) / 36.0),
                Tuple.Create(11, 34.76 / 36.0),
                Tuple.Create(11, 35.0 / 36.0),
                Tuple.Create(12, (35.0 + 1e-6) / 36.0),
                Tuple.Create(12, 35.956 / 36.0),
                Tuple.Create(12, 1.0),
            };
            var rng = new TestingRandomNumberGenerator();

            rng.AddFactorValues(rngExpectedValueAndProbability.Select(t => t.Item2));

            var rngStub = MockRepository.GenerateStub <IRandomNumberGenerator>();

            var dice = new[]
            {
                new NumericalDie(6, rngStub),
                new NumericalDie(6, rngStub),
            };
            var sumOf2D6 = new SumOfDice(dice, rng);

            foreach (Tuple <int, double> pair in rngExpectedValueAndProbability)
            {
                // Call
                int dieResult = sumOf2D6.Roll();

                // Assert
                Assert.AreEqual(pair.Item1, dieResult, $"Asserting for probability value {pair.Item2}.");
            }
        }