Skip to content

Instantly share code, notes, and snippets.

@itsho
Created March 19, 2017 12:22
Show Gist options
  • Save itsho/36a4b3c4c6b60ceb7a518f9021f02d4c to your computer and use it in GitHub Desktop.
Save itsho/36a4b3c4c6b60ceb7a518f9021f02d4c to your computer and use it in GitHub Desktop.
Simulating Monty Hall problem - running X times to get estimation by using Random
using System;
namespace MontyHallProblemSimulation
{
public class Program
{
private static Random _rnd = null;
private static char[] _arrDoors;
private const char DOOR_WITH_CAR = '*';
private const char DOOR_WITH_GOAT = '-';
private const int TOTAL_RUNS_EACH_CYCLE = 100000;
/// <summary>
/// if value is true, all messages will be displayed.
/// it will cause a delay when total cycles is larger than 1000
/// </summary>
private static bool blnWriteAllToScreen = false;
private static void WriteLine(string strLine)
{
if (blnWriteAllToScreen)
{
Console.WriteLine(strLine);
}
}
/// <summary>
/// this program will simulate Monty Hall issue.
/// will run TOTAL_RUNS_EACH_CYCLE times
/// </summary>
private static void Main(string[] args)
{
Console.WriteLine("============== Monty Hall problem simulation ===============");
Console.WriteLine("===== https://en.wikipedia.org/wiki/Monty_Hall_problem =====");
Console.WriteLine("Running 2 cycles. each one is " + TOTAL_RUNS_EACH_CYCLE + " tries...");
_rnd = new Random((int)DateTime.Now.Second);
int intCountWinsCase1 = 0;
for (int intRun = 0; intRun < TOTAL_RUNS_EACH_CYCLE; intRun++)
{
if (SingleCycle(false))
{
intCountWinsCase1++;
}
}
int intCountWinsCase2 = 0;
for (int intRun = 0; intRun < TOTAL_RUNS_EACH_CYCLE; intRun++)
{
if (SingleCycle(true))
{
intCountWinsCase2++;
}
}
Console.WriteLine(string.Format("When NOT Changing selection - User is right in {0} out of {1}. in percentage {2:0.00}", intCountWinsCase1, TOTAL_RUNS_EACH_CYCLE, (intCountWinsCase1 / (double)TOTAL_RUNS_EACH_CYCLE) * 100));
Console.WriteLine(string.Format("When DOES Changing selection - User is right in {0} out of {1}. in percentage {2:0.00}", intCountWinsCase2, TOTAL_RUNS_EACH_CYCLE, (intCountWinsCase2 / (double)TOTAL_RUNS_EACH_CYCLE) * 100));
}
private static bool SingleCycle(bool blnChangeDecision)
{
_arrDoors = new char[] { DOOR_WITH_GOAT, DOOR_WITH_GOAT, DOOR_WITH_GOAT };
// put car in random door
_arrDoors[_rnd.Next(3)] = DOOR_WITH_CAR;
WriteLine("there are 3 doors. please select door number (1/2/3):");
WriteLine("Door #");
int intUserSelection = GetFakeUserSelection();
WriteLine("\nDoor #" + (int)(intUserSelection+1) + " is selected by user");
int intEmptyDoor = TellEmptyDoor(intUserSelection);
WriteLine("Door #" + (int)(intEmptyDoor+1) + " is empty. do you want to change your selection?");
//var strUserWantTochange = Console.ReadLine();
//if (strUserWantTochange.ToLower() == "y")
if (blnChangeDecision)
{
// select other door
for (int intDoorIndex = 0; intDoorIndex < _arrDoors.Length; intDoorIndex++)
{
if (intDoorIndex != intUserSelection && intDoorIndex != intEmptyDoor)
{
intUserSelection = intDoorIndex;
WriteLine("Door #" + (int)(intUserSelection + 1) + " is NEW selection of the user");
break;
}
}
}
var blnUserIsCorrect = CheckIfUserIsCorrect(intUserSelection);
if (blnWriteAllToScreen)
{
if (blnUserIsCorrect)
{
Console.BackgroundColor = ConsoleColor.Green;
Console.ForegroundColor = ConsoleColor.White;
WriteLine("User is correct!");
}
else
{
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.White;
WriteLine("User is wrong");
}
Console.ResetColor();
}
return blnUserIsCorrect;
}
private static bool CheckIfUserIsCorrect(int intUserSelection)
{
return (_arrDoors[intUserSelection] == DOOR_WITH_CAR);
}
private static int TellEmptyDoor(int intUserSelection)
{
// tell user index of a single empty door
for (int intIndex = 0; intIndex < _arrDoors.Length; intIndex++)
{
if (intIndex != intUserSelection && _arrDoors[intIndex] == DOOR_WITH_GOAT)
{
return intIndex;
}
}
return -1;
}
private static int GetFakeUserSelection()
{
return _rnd.Next(3);
}
private static int GetRealUserSelection()
{
var strUserInput = Console.ReadLine();
var intSelection = Convert.ToInt32(strUserInput);
return intSelection - 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment