Jump to content

Monty Hall problem: Difference between revisions

m
→‎{{header|ANSI BASIC}}: Set line numbers.
m (→‎{{header|ANSI BASIC}}: Level of header lowered.)
m (→‎{{header|ANSI BASIC}}: Set line numbers.)
Line 546:
{{works with|Decimal BASIC}}
<syntaxhighlight lang="basic">
100 PROGRAM MontyHallProblem
110 DEF NGames = 10000
120 RANDOMIZE
130 LET NWins = 0
140 FOR Game = 0 TO NGames
150 IF IsGameWon(0) <> 0 THEN LET NWins = NWins + 1
160 NEXT Game
170 PRINT "NOT switching doors wins car in ";
180 PRINT USING "##.#": NWins / NGames * 100;
190 PRINT "% of games."
200 LET NWins = 0
210 FOR Game = 0 TO NGames
220 IF IsGameWon(1) <> 0 THEN LET NWins = NWins + 1
230 NEXT Game
240 PRINT "But switching doors wins car in ";
250 PRINT USING "##.#": NWins / NGames * 100;
260 PRINT "% of games."
270 END
280 REM ***
290 EXTERNAL FUNCTION IsGameWon(Sw)
300 REM Play one game.
310 REM Switching if and only if Sw <> 0.
320 REM Returns 1 if the game is won, 0 otherwise.
330 LET Car = INT(RND * 3) ! Randomly place car behind a door.
340 LET Player0 = INT(RND * 3) ! Player randomly chooses a door.
350 DO
360 LET Monty = INT(RND * 3) ! Monty opens door revealing a goat.
370 LOOP UNTIL (Monty <> Car) AND (Monty <> Player0)
380 IF Sw <> 0 THEN ! Player switches TO remaining door.
390 DO
400 LET Player = INT(RND * 3)
410 LOOP UNTIL (Player <> Player0) AND (Player <> Monty)
420 ELSE
430 LET Player = Player0 ! Player sticks with original door.
440 END IF
450 IF Player = Car THEN
460 LET IsGameWon = 1
470 ELSE
480 LET IsGameWon = 0
490 END IF
500 END FUNCTION
 
</syntaxhighlight>
{{out}}(example)
512

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.