local fmt = require "fmt" local function monty_hall(games) local switch_wins = 0 local stay_wins = 0 for i = 1, games do local doors = {0, 0, 0} -- all zero (goats) by default doors[math.random(3)] = 1 -- put car in a random door local choice = math.random(3) -- choose a door at random local shown = 0 while true do shown = math.random(3) -- the shown door if doors[shown] != 1 and shown != choice then break end end stay_wins += doors[choice] switch_wins += doors[6 - choice - shown] end fmt.print("Simulating %,s games:", games) fmt.print("Staying wins %,s times", stay_wins) fmt.print("Switching wins %,s times", switch_wins) end monty_hall(1_000_000)