Ejemplo n.º 1
0
 static void SimulateXCOMShotsOnDie(ISampler <int> d20, int minHitValue)
 {
     for (int i = 0; i < 12; i++)
     {
         for (int j = 0; j < Console.WindowWidth; j++)
         {
             Console.Write(d20.Next() > minHitValue ? '*' : '_');
         }
     }
     Console.WriteLine();
 }
        static void IllustrateCoinFlipRunsOnSampler(ISampler <bool> coin)
        {
            ConsoleColor oldColor = Console.ForegroundColor;

            ConsoleColor[] colors =
            {
                ConsoleColor.White,
                ConsoleColor.Gray,
                ConsoleColor.Green,
                ConsoleColor.DarkGreen,
                ConsoleColor.DarkMagenta,
                ConsoleColor.DarkRed,
                ConsoleColor.Red
            };
            bool lastRunValue  = coin.Next();
            int  lastRunLength = 0;

            for (int i = 0; i < 80; i++)
            {
                for (int j = 0; j < Console.WindowWidth; j++)
                {
                    bool value = coin.Next();
                    if (lastRunValue == value)
                    {
                        lastRunLength++;
                    }
                    else
                    {
                        lastRunLength = 0;
                    }
                    lastRunValue            = value;
                    Console.ForegroundColor = colors[Math.Min(colors.Length - 1, lastRunLength)];
                    Console.Write(value ? 'H' : 'T');
                }
            }
            Console.ForegroundColor = oldColor;
            Console.WriteLine();
        }
        private void Coin_ShowsDistribution(ISampler <bool> coin, double p, int n)
        {
            int trueCount  = 0;
            int falseCount = 0;

            for (int i = 0; i < n; i++)
            {
                if (coin.Next())
                {
                    trueCount++;
                }
                else
                {
                    falseCount++;
                }
            }

            double stddev         = Math.Sqrt(n * p * (1 - p));
            double stddevDistance = Math.Abs((double)trueCount - n * p) / stddev;

            Assert.IsTrue(Math.Abs(stddevDistance) < 3,
                          $"Coin sampler {stddevDistance} standard deviations from the mean");
        }