## “Monty Hall Problem” simulation in ZX Spectrum BASIC

Something completely different this week! I’ve been a bit of computer spod for most of my life, and it all started with a 48K ZX Spectrum home computer. My dad bought it in the early 80s and I took to writing games and keying in code listings from the likes of Your Computer and Your Sinclair. As a big fan of emulation and retro-computing, I’ve obviously acquired a Speccy simulator to relive the good old days.

Now, last week I was discussing the “Monty Hall Problem” with my missus. For the uninitiated, this is a probability problem that is often described in the context of a US game-show, where a contestant picks one of three doors. Behind one of these doors is the star prize, a car, with the other doors concealing a pair of undesirable goats. In the game, once the contestant has chosen a door, the host, the titular Monty Hall, reveals, at random, one of the goat doors and asks the contestant: “Do want to stick with your original choice or choose the other door?”. The Monty Hall problem suggests that the contestant has a 1 in 3 (33%) chance of winning with their original choice yet a 2 in 3 (66%) chance if they choose to swap. As such, the contestant should always choose to swap to have the best chance of winning the car. The problem can be proven mathematically, using Bayes Theorem, but is a fascinating problem to simulate using a simple computer program.

Combining these two interests, then, I’ve written a simulation of the Monty Hall Problem in BASIC on my simulated ZX Spectrum.

The solution is also available as .tap and .z80 files, which are available at the bottom of the listing. Next steps? To implement as a Siebel Workflow of course! 🙂

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
10 REM Monty Hall Problem 20 REM Iain Ollerenshaw 2018 30 RANDOMIZE 40 CLS 50 REM initialise counts. S = Swap, R=Stick 60 LET s=0: LET r=0 70 REM Get number of interations 80 INPUT "Num of iterations: ",t 90 FOR i=1 TO t 100 REM Initialise doors 110 DIM d$(3) 120 LET d$(1)="G" 130 LET d$(2)="G" 140 LET d$(3)="G" 150 REM place the car 160 LET c=INT (RND*3)+1 170 LET d$(c)="C" 180 GO SUB 360 190 REM Pick a door 200 LET d=INT (RND*3)+1 210 PRINT "I:";i;"+ D:";d;" C:";c;" R:";d$(d) 220 IF d=c THEN LET r=r+1 230 REM Monty reveals a random goat 240 LET m=INT (RND*3)+1 250 IF m=d OR m=c THEN GO TO 240 260 REM player changes doors 270 LET n=3*(m=1 AND d=2)+2*(m=1 AND d=3)+3*(m=2 AND d=1)+(m=2 AND d=3)+2*(m=3 AND d=1)+(m=3 AND d=2) 280 PRINT "I:";i;"* ND:";n;" C:";c;" R:";d$(n);" M:";m 290 IF n=c THEN LET s=s+1 300 REM prevent Scroll? message 310 POKE 23692,100 320 NEXT i 330 PRINT "Stick: ";r;" of ";t;" (";r/t*100;"%)" 340 PRINT "Swap: ";s;" of ";t;" (";s/t*100;"%)" 350 STOP 360 REM print the doors 370 PRINT d$(1);":";d$(2);":";d$(3) 380 RETURN |