pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

Lua pastebin - collaborative debugging tool View Help


Posted by Lucas Hermann Negri on Mon 20 Apr 00:49
report abuse | download | new post

  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))

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post