RosettaCodeData/Task/Monty-Hall-problem/SETL/monty-hall-problem.setl
2024-07-13 15:19:22 -07:00

29 lines
850 B
Text

program monty_hall;
setrandom(0);
n_simulations := 100000;
print('Chance to win:');
print('When switching doors:', win_chance(true, n_simulations) * 100, '%');
print('When not switching doors:', win_chance(false, n_simulations) * 100, '%');
proc win_chance(switch, n_simulations);
wins := 0;
loop for i in [1..n_simulations] do
wins +:= if simulate(switch) then 1 else 0 end;
end loop;
return wins / n_simulations;
end proc;
proc simulate(switch);
doors := {1, 2, 3};
car := random doors;
goats := doors less car;
choice := random doors;
opened := random (goats less choice);
if switch then
choice := arb (doors less choice less opened);
end if;
return choice = car;
end proc;
end program;