# Prove the Monty Hall Problem (via Simulation)!

• (5 Pages)
• « First
• 3
• 4
• 5

## 61 Replies - 26672 Views - Last Post: 03 March 2015 - 10:27 AM

### #61 Urganot

Reputation: 0
• Posts: 3
• Joined: 16-June 14

## Re: Prove the Monty Hall Problem (via Simulation)!

Posted 16 June 2014 - 07:20 AM

Nevermind. Just found the problem.
Now its only 1-2% different from the optimal result. I think that is ok.

Spoiler

### #62 greis09

Reputation: 1
• Posts: 1
• Joined: 03-March 15

## Re: Prove the Monty Hall Problem (via Simulation)!

Posted 03 March 2015 - 10:27 AM

I know that this thread is long gone , but I training c# so here is my answer:
IT gives the same percentage that everybody else (33% & 66%)

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MontyHall
{
class Program
{
static Random rnd = new Random();
static void Main(string[] args)
{
int sucessosTroca = 0, sucessoMantem = 0, total = 1000000;
Program prg = new Program();
for (int i = 0; i < total; i++)
{
if (prg.trocaPorta())
sucessosTroca++;
if (prg.MantemPorta())
sucessoMantem++;
}
Console.WriteLine("Em {2} simulações a troca de porta teve sucesso em {0} vezes, oque representa {1}%.\n", sucessosTroca,(sucessosTroca * 100) / total, total);
Console.WriteLine("Em {2} simulações manter a escolha de porta teve sucesso em {0} vezes, oque representa {1}%.\n", sucessoMantem, (sucessoMantem * 100) / total, total);
}

public bool trocaPorta()
{
string[,] portas = new string[,] {{"Ruim","" },{"Ruim","" },{"Ruim","" } };
int prtVenc  = rnd.Next(3);
int prtEsco = rnd.Next(3);

portas[prtVenc,0 ] = "Bom";
portas[prtEsco,1 ] = "Escolhida";

do
{
else

for (int i = 0; i < 3; i++)
{
//Console.Write("\n");
//Console.Write("Porta - " +i.ToString() + " premio -> "+ portas[i, 0] + " Situação - " + portas[i, 1]);

if (portas[i, 0] == "Bom" && portas[i, 1] == "")
{
//Console.Write("\nUsuário teria ganho.");
return true;
}
}
//Console.Write("\nFim de teste\n");
return false;
}
public bool MantemPorta()
{
int prtVenc = rnd.Next(3);
int prtEsco = rnd.Next(3);
if (prtVenc == prtEsco)
return true;
else
return false;
}
}
}

```