Monty Hall problem: Difference between revisions
Content added Content deleted
m (added whitespace before a header.) |
(→{{header|Elixir}}: change :random -> :rand module) |
||
Line 1,278: | Line 1,278: | ||
<lang elixir>defmodule MontyHall do |
<lang elixir>defmodule MontyHall do |
||
def simulate(n) do |
def simulate(n) do |
||
:random.seed(:os.timestamp) |
|||
{stay, switch} = simulate(n, 0, 0) |
{stay, switch} = simulate(n, 0, 0) |
||
:io.format "Staying wins ~w times (~.3f%)~n", [stay, 100 * stay / n] |
:io.format "Staying wins ~w times (~.3f%)~n", [stay, 100 * stay / n] |
||
Line 1,287: | Line 1,286: | ||
defp simulate(n, stay, switch) do |
defp simulate(n, stay, switch) do |
||
doors = Enum.shuffle([:goat, :goat, :car]) |
doors = Enum.shuffle([:goat, :goat, :car]) |
||
guess = : |
guess = :rand.uniform(3) - 1 |
||
[choice] = [0,1,2] -- [guess, shown(doors, guess)] |
[choice] = [0,1,2] -- [guess, shown(doors, guess)] |
||
if Enum.at(doors, choice) == :car, do: simulate(n-1, stay, switch+1), |
if Enum.at(doors, choice) == :car, do: simulate(n-1, stay, switch+1), |