Monty Hall problem: Difference between revisions
Content added Content deleted
(Added a task parallel Chapel solution) |
(Added Elixir) |
||
Line 1,271: | Line 1,271: | ||
</pre> |
</pre> |
||
=={{header|Elixir}}== |
|||
<lang elixir>defmodule MontyHall do |
|||
def simulate(n) do |
|||
:random.seed(:os.timestamp) |
|||
{stay, switch} = simulate(n, 0, 0) |
|||
:io.format "Staying wins ~w times (~.3f%)~n", [stay, 100 * stay / n] |
|||
:io.format "Switching wins ~w times (~.3f%)~n", [switch, 100 * switch / n] |
|||
end |
|||
defp simulate(0, stay, switch), do: {stay, switch} |
|||
defp simulate(n, stay, switch) do |
|||
doors = Enum.shuffle([:goat, :goat, :car]) |
|||
guess = :random.uniform(3) - 1 |
|||
[choice] = [0,1,2] -- [guess, shown(doors, guess)] |
|||
if Enum.at(doors, choice) == :car, do: simulate(n-1, stay, switch+1), |
|||
else: simulate(n-1, stay+1, switch) |
|||
end |
|||
defp shown(doors, guess) do |
|||
[i, j] = Enum.shuffle([0,1,2] -- [guess]) |
|||
if Enum.at(doors, i) == :goat, do: i, else: j |
|||
end |
|||
end |
|||
MontyHall.simulate(10000)</lang> |
|||
{{out}} |
|||
<pre> |
|||
Staying wins 3397 times (33.970%) |
|||
Switching wins 6603 times (66.030%) |
|||
</pre> |
|||
=={{header|Emacs Lisp}}== |
=={{header|Emacs Lisp}}== |