Guest
Public paste!

Lucas Hermann Negri

By: a guest | Apr 19th, 2009 | Syntax: Lua | Size: 0.71 KB | Hits: 176 | Expires: Never
Copy text to clipboard
  1. math.randomseed(os.time())
  2.  
  3. function testa(troca)
  4.         local portas = {false, false, false}
  5.         portas[math.random(3)] = true
  6.        
  7.         -- Assume-se que escolheu a 1, e remove-se ou a 2 ou a 3
  8.         local r = 2
  9.         if portas[2] then r = 3 end
  10.         table.remove(portas, r)
  11.        
  12.         -- Verifica se o jogador ganhou
  13.         if not troca then
  14.                 return portas[1]
  15.         else
  16.                 return portas[2]
  17.         end
  18. end
  19.  
  20. -- Simula N vezes
  21. local trocar = arg[1] == "trocar"
  22. local total, venceu = 1000000, 0
  23. print(string.format("Simulando %i jogos, %s de porta", total, trocar and "trocando" or "sem trocar"))
  24.  
  25. for i = 1, total do
  26.         if testa(trocar) then
  27.                 venceu = venceu + 1
  28.         end
  29. end
  30.  
  31. print(string.format("Resultado: ganhou %.2f%% das vezes!", venceu / total))