Prove the Monty Hall Problem (via Simulation)!

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

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

#61 Urganot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#62 greis09  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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);
			Console.Read();
		}

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

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

			do
			{
				if (portas[eliminada, 1] == "Escolhida" || portas[eliminada, 0] == "Bom")
					eliminada = rnd.Next(3);
				else
					portas[eliminada, 1] = "Eliminada";
			}while (portas[eliminada, 1] != "Eliminada");

			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;
		}
	}
}



Was This Post Helpful? 1
  • +
  • -

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