Monty Hall problem: Difference between revisions

Content added Content deleted
(added Fortran)
(Added BASIC)
Line 165: Line 165:
Algorithm random: prize count = 4991, = 49.91%
Algorithm random: prize count = 4991, = 49.91%
bash$</pre>
bash$</pre>
=={{header|BASIC}}==

{{works with|QuickBasic|4.5}}
<qbasic>RANDOMIZE TIMER
DIM doors(3) '0 is a goat, 1 is a car
CLS
switchWins = 0
stayWins = 0
FOR plays = 0 TO 32767
winner = INT(RND * 3) + 1
doors(winner) = 1'put a winner in a random door
choice = INT(RND * 3) + 1'pick a door, any door
DO
shown = INT(RND * 3) + 1
'don't show the winner or the choice
LOOP WHILE doors(shown) <> 1 AND shown <> choice
stayWins = stayWins + doors(choice) 'if you won by staying, count it
'could have switched to win
IF doors(choice) = 0 THEN
switchWins = switchWins + 1
END IF
doors(winner) = 0 'clear the doors for the next test
NEXT plays
PRINT "Switching wins"; switchWins; "times."
PRINT "Staying wins"; stayWins; "times."</qbasic>
Output:
<pre>Switching wins 21805 times.
Staying wins 10963 times.</pre>
=={{header|Fortran}}==
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
{{works with|Fortran|90 and later}}