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

33 lines
1.1 KiB
Text

def NGames = 10000; \number of games simulated
int Game, NWins;
include c:\cxpl\codes;
func int IsGameWon(Switch); \Play one game
int Switch;
int Car, Player, Player0, Monty;
[Car:= Ran(3); \randomly place car behind a door
Player0:= Ran(3); \player randomly chooses a door
repeat Monty:= Ran(3); \Monty opens door revealing a goat
until Monty # Car and Monty # Player0;
if Switch then \player switches to remaining door
repeat Player:= Ran(3);
until Player # Player0 and Player # Monty
else Player:= Player0; \player sticks with original door
return Player = Car;
];
[Format(2,1);
NWins:= 0;
for Game:= 0 to NGames-1 do
if IsGameWon(false) then NWins:= NWins+1;
Text(0, "NOT switching doors wins car in ");
RlOut(0, float(NWins)/float(NGames)*100.0);
Text(0, "% of games.^M^J");
NWins:= 0;
for Game:= 0 to NGames-1 do
if IsGameWon(true) then NWins:= NWins+1;
Text(0, "But switching doors wins car in ");
RlOut(0, float(NWins)/float(NGames)*100.0);
Text(0, "% of games.^M^J");
]