Esempio n. 1
0
        public void GetProbabilityCoinFlipTest()
        {
            // Flip a coin TEST_ITERATION times.  On balance, it should return
            // true ("heads") %PROBABILITY of the time, + or - %MARGIN_OF_ERROR.

            const Int32  TEST_ITERATIONS        = 100000;
            const Double MARGIN_OF_ERROR        = 0.01d; // One percent.
            const Double PROBABILITY            = 0.38d; // # between 0 and 1 exclusive.
            const Double LOWER_ACCEPTABLE_BOUND = PROBABILITY - MARGIN_OF_ERROR;
            const Double UPPER_ACCEPTABLE_BOUND = PROBABILITY + MARGIN_OF_ERROR;

            Int32 numberOfHeadsFlips = 0;

            // Count the number of "heads" results.
            for (Int32 i = 1; i <= TEST_ITERATIONS; i++)
            {
                numberOfHeadsFlips += (RandomRoutines.GetCoinFlip((Double)PROBABILITY) ? 1 : 0);
            }

            var percentOfHeadsFlips = (Double)numberOfHeadsFlips / (Double)TEST_ITERATIONS;

            var success = (
                (LOWER_ACCEPTABLE_BOUND < percentOfHeadsFlips) &&
                (percentOfHeadsFlips < UPPER_ACCEPTABLE_BOUND));

            Assert.IsTrue(
                success,
                String.Format("{0} out of {1} probability coin flips ({2} percent) returned true.  Target was {3}% (+/- {4}%).",
                              numberOfHeadsFlips, TEST_ITERATIONS, percentOfHeadsFlips * 100, PROBABILITY * 100, MARGIN_OF_ERROR * 100));
        }
Esempio n. 2
0
        public void GetCoinFlipTest()
        {
            // Flip a coin TEST_ITERATION times.  On balance, it should return
            // true ("heads") %50 of the time, + or - %MARGIN_OF_ERROR.

            const Int32  TEST_ITERATIONS        = 100000;
            const Double MARGIN_OF_ERROR        = 0.01d; // One percent.
            const Double LOWER_ACCEPTABLE_BOUND = 0.5d - MARGIN_OF_ERROR;
            const Double UPPER_ACCEPTABLE_BOUND = 0.5d + MARGIN_OF_ERROR;

            Int32 numberOfHeads = 0;

            for (Int32 i = 1; i <= TEST_ITERATIONS; i++)
            {
                numberOfHeads += (RandomRoutines.GetCoinFlip() ? 1 : 0);
            }

            var percentOfHeadsFlips = (Double)numberOfHeads / (Double)TEST_ITERATIONS;

            var success = ((LOWER_ACCEPTABLE_BOUND < percentOfHeadsFlips) && (percentOfHeadsFlips < UPPER_ACCEPTABLE_BOUND));

            Assert.IsTrue(
                success,
                String.Format("{0} out of {1} coin flips ({2}%) returned true.  Target was 50% (+/- {3}%).",
                              numberOfHeads, TEST_ITERATIONS, percentOfHeadsFlips * 100, MARGIN_OF_ERROR * 100));
        }