The drinking game problem
Hot on the heels of the 100 door problem, here is a monte carlo simulation for the drinking game problem as described by Mr Barton Maths and others.
You have 6 empty glasses numbered 1 to 6. You roll a standard die. If the number for the glass is empty, then the glass is filled with frothy bilge(*). If the number for the glass is full, you drink it.
There is a special rule when 5 glasses are full of lovely frothy bilge. If you roll the number for the empty glass, then the final glass gets filled and you have to drink all 6 glasses. The game then ends.
The problem: What is the average number of rolls needed until a game ends – always assuming that your liver and those of your drinking buddies can survive? (Hint: don’t try this game in real life).
It’s straightforward enough to solve this problem using probability theory and some simultaneous equations, but where’s the fun in that?
Spoiler alert – the actual answer to the problem (but not the method) is given in the paragraph after the first screenshot and video, so don’t scroll beyond them if you’re going to attempt it!
Instead, here’s a monte carlo algorithm to do the same thing, written for the Sharp MZ-700. (The character set on modern machines and t’interweb isn’t quite up to dealing with the MZ-700 character set, but the listing approximates it. The circled “C” in a print statement clears the screen; circled “H” non-destructively positions the cursor at the top left).
10 REM ******************************
12 REM * *
14 REM * Drinking game puzzle *
16 REM * *
18 REM * You have six empty glasses *
20 REM * and a die. If you roll the *
22 REM * number of an empty glass *
24 REM * it is filled, if you roll *
26 REM * a full glass you drink it. *
28 REM * *
30 REM * If you have five full *
32 REM * glasses and roll the empty *
34 REM * glass it is filled, you *
36 REM * drink all six, and the *
38 REM * game ends. *
40 REM * *
42 REM * What is the average number *
44 REM * of rolls needed to end the *
46 REM * game? *
48 REM * *
50 REM * *
52 REM * Tim Holyoake, 08/08/2021 *
54 REM * *
56 REM ******************************
60 PRINT "Ⓒ⍗⍗⍈⍈⍈⍈Drinking game puzzle"
70 PRINT "⍈⍈⍈⍈====================="
100 REM Set up the glasses
110 FOR G = 1 TO 6
120 POKE 53766+G*6,113
130 POKE 53806+G*6,113
140 POKE 53846+G*6,113
150 POKE 53886+G*6,112:POKE 53887+G*6,112
160 POKE 53847+G*6,61
170 POKE 53807+G*6,61:POKE 53808+G*6,105
180 POKE 53767+G*6,61
190 POKE 53927+G*6,32+G
200 NEXT G
210 PRINT"Ⓗ⍗⍗⍗⍗⍗⍗⍗⍗⍗⍗⍗⍗⍗⍗⍗⍗⍗⍗⍗⍗⍈⍈⍈⍈Glass state: "
220 GOSUB 1000
250 REM Number of iterations
260 TT=0:LG=0:SG=1000000:TI$="000000"
265 DR = 0: FG = 0
270 FOR I = 1 TO 1000
300 REM Play the game
320 REM Roll the die
330 D = INT(RND(1)*6)+1
340 IF PEEK(53726+D*6)<>212 GOTO 500
350 REM Drink!
360 POKE 53726+D*6,0:POKE 53727+D*6,0
370 DR=DR+1:FG=FG-1
380 GOSUB 1000:GOTO 300
500 REM Fill my glass please!
510 POKE 53726+D*6,212:POKE 53727+D*6,212
520 DR=DR+1:FG=FG+1
530 IF (FG=6) THEN GOTO 600
540 GOSUB 1000:GOTO 300
600 REM Game over - hic!
606 GOSUB 1000:MUSIC "C0"
610 TT=TT+DR
612 IF (DR<SG) THEN SG=DR
614 IF (DR>LG) THEN LG=DR
620 FOR G = 1 TO 6
630 POKE 53726+G*6,0:POKE 53727+G*6,0
640 NEXT G
660 PRINT "Ⓗ⍗⍗⍗⍗⍗⍈⍈⍈⍈Iteration ";I
670 PRINT "⍈⍈⍈⍈Rolls needed ";DR;" "
672 PRINT "⍗⍈⍈⍈⍈Least rolls ";SG;" "
674 PRINT "⍈⍈⍈⍈Most rolls ";LG;" "
680 PRINT "⍈⍈⍈⍈Average rolls ";INT((TT/I)+.5);" "
690 PRINT "⍗⍗⍗⍗⍗⍗⍗⍗⍗⍗⍗⍗⍈⍈⍈⍈Elapsed time ";LEFT$(TI$,2);":";MID$(TI$,3,2);":";RIGHT$(TI$,2)
700 FG=0:DR=0:GOSUB 1000
800 NEXT I
999 END
1000 REM Glass state display subr
1010 IF (FG=0)+(FG=6) GOTO 1050
1020 FOR S=1TOFG:POKE 54064+S,71:NEXT S
1030 FOR S=FG+1TO6:POKE 54064+S,72: NEXT S
1040 RETURN
1050 IF (FG=6) GOTO 1080
1060 FOR S=1TO6:POKE 54064+S,72:NEXT S
1070 RETURN
1080 FOR S=1TO6:POKE 54064+S,71:NEXT S
1090 RETURN
If you want to try the code on an emulator, there’s a mzf version in my MZ-700 github repository.
A number of implementation decisions are worth commenting on. Firstly, I’m calculating an integer average number of die rolls as that seems more realistic. (The actual answer to the problem isn’t a whole number of rolls however).
Secondly, I’ve decided that 1,000 iterations should be sufficient to get reasonably close to the actual answer.
Thirdly, I’ve got a glass state display subroutine from line 1000 onwards. Not strictly necessary, but it gives insight into the progress of the game and tidied my original code up a little.
Fourthly, I’ve avoided S-BASIC extensions so the code should run unaltered using SP-5025 on an MZ-80K.
Finally, I’ve chosen to use the method of peeking the screen memory to determine if a glass is full or not at line 340. Other methods are available, but it stays true to the 100 door problem inspiration!
Here’s a picture of the simulation running after 106 iterations. Do you think 73 is close to the correct answer?

And here’s the result after all 1,000 iterations of the game. The monte carlo simulation suggests 85 rolls of the die are required on average to complete the game (although the range of possibilities is enormous – potentially infinite if you’re really unlucky). This is ‘close enough’ to the actual answer (83.2) for me to be happy with the result.

Another wrinkle is that the average calculated above is an odd number. In reality, you can only finish this game with an even number of rolls of the die.
(*) You’ll only understand the reference to “frothy bilge” if you were in the improvised “Showstoppers” performance that I went to at the Edinburgh Fringe some years ago. But think of what might substitute for beer in the forthcoming post-Brexit shortages and you won’t be too far away.