Monty Hall problem: Difference between revisions

no edit summary
(→‎version 3: added REXX version three (to support any number of doors), and made version 2 consistent with version 3, aligned some statements.)
No edit summary
Line 2,401:
Wins when changing: 6567
%1 = [3433, 6567]</pre>
 
=={{header|Pascal}}==
<lang pascal>program MontyHall;
 
uses
sysutils;
 
const
NumGames = 1000;
 
 
{Randomly pick a door(a number between 0 and 2}
function PickDoor(): Integer;
begin
Exit(Trunc(Random * 3));
end;
 
var
i: Integer;
PrizeDoor: Integer;
ChosenDoor: Integer;
WinsChangingDoors: Integer = 0;
WinsNotChangingDoors: Integer = 0;
begin
Randomize;
for i := 0 to NumGames - 1 do
begin
//randomly picks the prize door
PrizeDoor := PickDoor;
//randomly chooses a door
ChosenDoor := PickDoor;
 
//if the strategy is not changing doors the only way to win is if the chosen
//door is the one with the prize
if ChosenDoor = PrizeDoor then
Inc(WinsNotChangingDoors);
 
//if the strategy is changing doors the only way to win is if we choose one
//of the two doors that hasn't the prize, because when we change we change to the prize door.
//The opened door doesn't have a prize
if ChosenDoor <> PrizeDoor then
Inc(WinsChangingDoors);
end;
 
Writeln('Num of games:' + IntToStr(NumGames));
Writeln('Wins not changing doors:' + IntToStr(WinsNotChangingDoors) + ', ' +
FloatToStr((WinsNotChangingDoors / NumGames) * 100) + '% of total.');
 
Writeln('Wins changing doors:' + IntToStr(WinsChangingDoors) + ', ' +
FloatToStr((WinsChangingDoors / NumGames) * 100) + '% of total.');
 
end.
</lang>
 
Output:
<pre>Num of games:1000
Wins not changing doors:359, 35,9% of total.
Wins changing doors:641, 64,1% of total.</pre>
 
=={{header|Perl}}==