RosettaCodeData/Task/Monty-Hall-problem/Loglan82/monty-hall-problem.loglan82
2026-04-30 12:34:36 -04:00

48 lines
1.2 KiB
Text

program MontyHall;
(* Monty Hall problem *)
const
n_games = 10000;
unit is_game_won: function (sw: boolean): boolean;
var
car, player, player0, Monty: integer;
begin
car := entier(random * 3); (* Randomly place car behind a door. *)
player0 := entier(random * 3); (* Player randomly chooses a door. *)
Monty := entier(random * 3); (* Monty opens door revealing a goat. *)
while (Monty = car) or (Monty = player0)
do
Monty := entier(random * 3)
od;
if sw
then (* Player switches to remaining door. *)
player := entier(random * 3);
while (player = player0) or (player = Monty)
do
player := entier(random * 3)
od
else
player := player0 (* Player sticks with original door. *)
fi;
result := (player = car)
end;
var
n_wins, game: integer;
begin
n_wins := 0;
for game := 1 to n_games
do
if is_game_won(false) then n_wins := n_wins + 1 fi
od;
write("NOT switching doors wins car in ");
writeln(n_wins / n_games * 100: 4: 1, "% of games.");
n_wins := 0;
for game := 1 to n_games
do
if is_game_won(true) then n_wins := n_wins + 1 fi
od;
write("But switching doors wins car in ");
writeln(n_wins / n_games * 100: 4: 1, "% of games.");
end