Skip to content

Instantly share code, notes, and snippets.

@XDRosenheim
Created February 15, 2016 13:45
Show Gist options
  • Save XDRosenheim/407e5acc54ba66eb4131 to your computer and use it in GitHub Desktop.
Save XDRosenheim/407e5acc54ba66eb4131 to your computer and use it in GitHub Desktop.
Is it better to change door at the Monty Hall problem?
using System;
namespace MontyHall {
internal class Program {
private static void Main() {
var rnd = new Random();
bool[] doors = { false, false, false };
const int iterations = 10000000;
var counter = 0;
var winsOnChange = 0;
var winsOnStaying = 0;
while(counter < iterations) {
doors[0] = false;
doors[1] = false;
doors[2] = false;
doors[rnd.Next( 1, 4 ) - 1] = true;
if(doors[rnd.Next( 1, 4 ) - 1]) {
winsOnStaying++;
}
counter++;
}
counter = 0;
while(counter < iterations) {
doors[0] = false;
doors[1] = false;
doors[2] = false;
doors[rnd.Next( 1, 4 ) - 1] = true;
if(!doors[rnd.Next( 1, 4 ) - 1]) {
winsOnChange++;
}
counter++;
}
Console.WriteLine( "Change door: \t" + winsOnChange + " / " + iterations );
Console.WriteLine( "Same door: \t" + winsOnStaying + " / " + iterations );
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment