RosettaCodeData/Task/Monty-Hall-problem/FutureBasic/monty-hall-problem.basic

61 lines
1.2 KiB
Plaintext

// Monty Hall problem
// May 2024 - Rich Love
local fn IsGameWon(Sw as int) as bool
REM Play one game.
REM Switching if and only if Sw <> 0.
REM Returns 1 if the game is won, 0 otherwise.
short Car = INT(RND(3)) //Randomly place car behind a door.
short Player0 = INT(RND(3)) // Player randomly chooses a door.
short Monty
short IsGameWon
short Player
DO
Monty = INT(RND(3)) // Monty opens door revealing a goat.
UNTIL (Monty <> Car) AND (Monty <> Player0)
380
IF Sw <> 0 // Player switches TO remaining door.
DO
Player = INT(RND(3))
UNTIL (Player <> Player0) AND (Player <> Monty)
Monty = INT(RND(3))
ELSE
Player = Player0 // Player sticks with original door.
END IF
IF Player = Car
IsGameWon = 1
ELSE
IsGameWon = 0
END IF
END fn = IsgameWon
_NGames = 10000
float NWins = 0
short Game
FOR Game = 0 TO _NGames
IF fn IsGameWon(0) <> 0 THEN NWins = NWins + 1
NEXT Game
PRINT "NOT switching doors wins car in ";
PRINT USING "##.##" ; NWins / 100 ;
PRINT " % of games."
NWins = 0
FOR Game = 0 TO _NGames
IF fn IsGameWon(1) <> 0 THEN NWins = NWins + 1
NEXT Game
PRINT "But switching doors wins car in ";
PRINT USING "##.##" ; NWins / _NGames * 100;
PRINT " % of games."
handleevents