Monty Hall problem: Difference between revisions

no edit summary
(Simpler D code)
No edit summary
Line 2,367:
Staying winns: 3354
Switching winns: 6646
</pre>
 
=={{header|XPL0}}==
<lang XPL0>def Games = 10000; \number of games simulated
int Game, Wins;
include c:\cxpl\codes;
 
proc Play(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
if Player = Car then Wins:= Wins+1;
];
 
[Format(2,1);
Text(0, "Not switching doors wins car in ");
Wins:= 0;
for Game:= 0 to Games-1 do Play(false);
RlOut(0, float(Wins)/float(Games)*100.0);
Text(0, "% of games.^M^J");
 
Text(0, "But switching doors wins car in ");
Wins:= 0;
for Game:= 0 to Games-1 do Play(true);
RlOut(0, float(Wins)/float(Games)*100.0);
Text(0, "% of games.^M^J");
]</lang>
 
Example output:
<pre>
Not switching doors wins car in 33.7% of games.
But switching doors wins car in 66.7% of games.
</pre>
772

edits