2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
50
Task/Monty-Hall-problem/Pascal/monty-hall-problem.pascal
Normal file
50
Task/Monty-Hall-problem/Pascal/monty-hall-problem.pascal
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
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.
|
||||
Loading…
Add table
Add a link
Reference in a new issue