public void MeanTest() { var a = new Accumulator(); double total = 0; a.AddDataValue(1.2); total += 1.2; a.AddDataValue(1.5); total += 1.5; Assert.Equal(total / 2, a.Mean); }
static void Main(string[] args) { Console.WriteLine("Chameleons simulation."); // a list of chameleons List <Chameleon> cc = new List <Chameleon>(); // how many of each color do we need int nBlack = 15; int nBrown = 10; int nGray = 14; // how many simulations to run int nSimulations = 10000; // create an accumulator keep track of how many generations to all same color Accumulator runCounts = new Accumulator(); // number of generations for which no solution was found int nNoSolution = 0; // a random number generator Random r = new Random(); // keep track of which color is the final color Dictionary <Chameleon.colors, int> fnlColors = new Dictionary <Chameleon.colors, int>(); fnlColors[Chameleon.colors.Black] = 0; fnlColors[Chameleon.colors.Brown] = 0; fnlColors[Chameleon.colors.Gray] = 0; int g; // count of chameleon meetings needed to reach all same color bool allSameColor; // true if all same color after run Chameleon.colors fnlColor; // final color if all same color reached // run the simulations in a loop for (int i = 0; i < nSimulations; i++) { g = 0; Initialize(cc, nBlack, nBrown, nGray); RunSimulation(cc, r, out g, out allSameColor, out fnlColor); if (allSameColor) { runCounts.AddDataValue(g); switch (fnlColor) { case Chameleon.colors.Black: fnlColors[Chameleon.colors.Black] += 1; break; case Chameleon.colors.Brown: fnlColors[Chameleon.colors.Brown] += 1; break; case Chameleon.colors.Gray: fnlColors[Chameleon.colors.Gray] += 1; break; default: break; } } else { nNoSolution++; } cc.Clear(); } Console.WriteLine("Results of simulations"); Console.WriteLine("Number of simulations: {0}", nSimulations); Console.WriteLine("Number of simulations with no solution: {0}", nNoSolution); Console.WriteLine("Average number of meetings to all chameleons are of " + " the same color {0}\n", runCounts.Mean()); Console.WriteLine("Final color Black: {0}", fnlColors[Chameleon.colors.Black]); Console.WriteLine("Final color Brown: {0}", fnlColors[Chameleon.colors.Brown]); Console.WriteLine("Final color Gray: {0}", fnlColors[Chameleon.colors.Gray]); Console.WriteLine("Fewest number of meetings until all same color: {0}", runCounts.Lo()); Console.WriteLine("Greatest number of meetings until all same\ncolor " + " (if solution was found): {0}", runCounts.Hi()); Console.WriteLine("Press any key to continue."); Console.ReadKey(); }
static void Main(string[] args) { // Project Euler Problem 280 Ant and Seeds // Author: D. // Date: 11/26/2014 // // https://projecteuler.net/problem=280 // Ant and seeds // Problem 280: // "A laborious ant walks randomly on a 5x5 grid. The walk starts from the central square. // At each step, the ant moves to an adjacent square at random, without leaving the grid; thus there // are 2, 3 or 4 possible moves at each step depending on the ant's position. // At the start of the walk, a seed is placed on each square of the lower row. When the ant // isn't carrying a seed and reaches a square of the lower row containing a seed, it // will start to carry the seed. The ant will drop the seed on the first empty square of // the upper row it eventually reaches. // What's the expected number of steps until all seeds have been dropped in the top row? // Give your answer rounded to 6 decimal places." // Windows .NET console application in C#. // Created with Windows 10, .NET framework 4.6, and Visual Studio Community Edition 2015 // Solution - Brute force approach: run a set of simulations keeping track of number // of moves required to move all the seeds to their final position. // When all the simulations have been run, find the average number of moves // and display on screen. // Assumptions or inferences from the problem statement: // "Ant can only move to adjacent square" - must mean no diagonal moves. // Ant can only carry one seed at a time. // Ant may only drop seed in top row. // Ant can move to cell containing a seed, even if it already has a seed. // The grid has 'walls' - the ant never tries to move to a cell outside the grid. // This was an exercise in C# development. // Solution was not checked against official number on Project Euler site. Console.WriteLine("Project Euler Problem 280: Ant and Seeds"); const int numRows = 5; //num rows in the grid const int numCols = 5; //num cols in the grid //create an array to hold the count of moves from each simulation run. Array size = number of simulations to run. const double nbrSimulationsToRun = 1000; double nbrMoves; // create the ant and place it in the grid on which the ant moves in the simulations AntGrid antGrid = new AntGrid(numRows, numCols); Accumulator sumOfMoves = new Accumulator(); for (int i = 0; i < nbrSimulationsToRun; i++) { // initialize and center the ant on the grid antGrid.InitializeGrid(); //antGrid.DisplayGrid(numRows, numCols, grid, ant); // run a simulation nbrMoves = antGrid.RunSimulation(); sumOfMoves.AddDataValue(nbrMoves); }; Console.WriteLine("The average number of moves in {0} simulations was: {1:f6}", nbrSimulationsToRun, sumOfMoves.Mean()); //antGrid.DisplayGrid(numRows, numCols, grid, ant); // keep console window open until user is ready to close out. Console.WriteLine("Complete. Press any key to exit."); Console.ReadKey(); }