29 lines
850 B
Text
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;
|