# 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.

### 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.