Presidents and Terms – A Monte Carlo exercise using c# , .net
Consider the 24 presidents (and their corresponding 24 terms in office) in the 19th century. A student was asked to match the president to his term. Not knowing the answers, the student resorted to guessing. He had two possible strategies – one was to keep guessing the same term for every president – he was bound to get ONE right. The other was to randomly guess a term when presented with the name of a president. How many guesses would actually be correct – on average?
An analytical solution
Is difficult. While it is possible to solve this problem analytically, it is not necessary to do so. One can arrive at the answer using another technique – detailed below.
A Monte Carlo Approach (using C# , .NET)
Imagine that instead of ONE student, there were 10,000 students – and each of them had a chance at playing this guessing game. As each student finished his/her game, we record the number of ‘hits’ – the number of guesses they got correct. We do this for each of the 10,000 students. The underlying hope is that averaging over so many students gets us closer to an accurate ‘number of hits’ for ONE student. The average of 10,000 students should serve as a ‘close to accurate’ answer.
This is the Monte Carlo approach. We ‘simulate’ multiple trials of the random process – so that the randomness is ‘averaged’ out. The more data points we average over, the more accurate our answer.
Here is some c# code showing the monte carlo simulation.
Code Snippet
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace ConsoleApplication1
- {
- class Program
- {
- static int totalTerms = 24;
- static int success = 0;
- static int totalIterations = 10000;
- static Random r = new Random();
-
- static void Main(string[] args)
- {
- for(int j = 0; j< totalIterations; j++)
- {
- innerLoop(j);
- }
- Console.WriteLine("Total successes in " + totalIterations + " = " + success);
- float probability = success / totalIterations;
- Console.WriteLine("Each student guesses correctly with probability = " + probability);
-
- }
-
- static void innerLoop(int iterNum)
- {
- for(int i=0;i<totalTerms; i++)
- {
- int randTerm = pickRandTerm();
- if (randTerm == i)
- {
- success++;
- }
- }
- }
-
- static int pickRandTerm()
- {
- return r.Next(totalTerms);
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication1
- {
- class Program
- {
- static int totalTerms = 24;
- static int success = 0;
- static int totalIterations = 10000;
- static Random r = new Random();
- static void Main(string[] args)
- {
- for(int j = 0; j< totalIterations; j++)
- {
- innerLoop(j);
- }
- Console.WriteLine("Total successes in " + totalIterations + " = " + success);
- float probability = success / totalIterations;
- Console.WriteLine("Each student guesses correctly with probability = " + probability);
- }
- static void innerLoop(int iterNum)
- {
- for(int i=0;i<totalTerms; i++)
- {
- int randTerm = pickRandTerm();
- if (randTerm == i)
- {
- success++;
- }
- }
- }
- static int pickRandTerm()
- {
- return r.Next(totalTerms);
- }
- }
- }
Download full solution (.zip)
Summary
Monte Carlo simulations are a computing approach used to solve tough mathematical problems – problems that typically have no analytical solution. These problems typically have randomness built into them. The simulation approach essentially ‘averages out’ the randomness – providing an answer as close to the exact probability as possible.
About the Author
A Microsoft .NET Architect, Anuj Varma has been fortunate to be a .NET architect on some of the largest projects across Texas. These include Fortune 50 customers building cloud and SOA applications as well as SQL Server migrations. Anuj continues to work (in the capacity of a .NET architect) with Texas companies – both startups as well as Fortune 500 companies – that have adopted Microsoft technologies.
Leave a Reply