Lucas Hermann Negri
By: a guest | Apr 19th, 2009 | Syntax:
Lua | Size: 0.71 KB | Hits: 176 | Expires: Never
math.randomseed(os.time())
function testa(troca)
local portas = {false, false, false}
portas[math.random(3)] = true
-- Assume-se que escolheu a 1, e remove-se ou a 2 ou a 3
local r = 2
if portas[2] then r = 3 end
table.remove(portas, r)
-- Verifica se o jogador ganhou
if not troca then
return portas[1]
else
return portas[2]
end
end
-- Simula N vezes
local trocar = arg[1] == "trocar"
local total, venceu = 1000000, 0
print(string.format("Simulando %i jogos, %s de porta", total, trocar and "trocando" or "sem trocar"))
for i = 1, total do
if testa(trocar) then
venceu = venceu + 1
end
end
print(string.format("Resultado: ganhou %.2f%% das vezes!", venceu / total))