Dice frequency

Dice frequency
Photo by Josh Appel / Unsplash

I've been obsessed with the Random number generator on the Commodore computers for a LONG time, and I'm not the only one who is, to be sure.

We've used this random function in many of the Cipher series articles before, both via BASIC and directly getting randomness from the SID oscillator 3.

Let's try a "practical" example we might code into a game that throws dice and tests the frequency distribution. For one or two dice, we can easily calculate what a "perfect" distribution should be, so this should be a good test to see if the random number generator is sufficient to make a fair game.

Single

A single six-sided is pretty easy to understand, and there's a 1 in 6 chance of rolling any of the sides. So a proper frequency distribution should come in at 16.67%.

Let's try it 1000 and then 10000 times on a Commodore 64 to see how random the random number generation is in a "practical example."

5 PRINTCHR$(147):PRINT"ROLLING DICE..."
10 TI$="000000"
20 DIMT(6):N=1000
100 FORI=0TON
110 S=INT(RND(.)*6)+1
120 T(S)=T(S)+1
130 NEXT
200 PRINTCHR$(147)
210 PRINT"DICE    FREQUENCY    PERCENTAGE"
220 PRINT"-------------------------------"
230 PRINT:FORJ=1TO6:PRINTJ;TAB(9);T(J);TAB(21);T(J)/N*100;"%":NEXT
1100 PRINT:PRINT"PROCESS TIME:"TI/60

When run 1000 times:

N=1000

And it gets slightly better at 10000 iterations:

N=10000

Two dice

Two dice are also pretty easy to calculate. There's a 1/36 chance of any of the possible combinations.

Dice CombinedCombinationsProbaility%
21+11/362.78%
31+2 or 2+12/365.56%
41+3 or 2+2 or 3+13/368.33%
51+4 or 2+3 or 3+2 or 4+14/3611.11%
61+5 or 2+4 or 3+3 or 4+2 or 5+15/3613.89%
71+6 or 2+5 or 3+4 or 4+3 or 5+2 or 6+16/3616.67%
82+6 or 3+5 or 4+4 or 5+3 or 6+25/3613.89%
93+6 or 4+5 or 5+4 or 6+34/3611.11%
104+6 or 5+5 or 6+43/368.33%
115+6 or 6+52/365.56%
126+61/362.78%

The modifications are simple. Slight change to lines 20 and 230, and we can get the output we're looking for.

5 PRINTCHR$(147):PRINT"ROLLING DICE..."
10 TI$="000000"
20 DIMT(12):N=1000
100 FORI=0TON
110 S=INT(RND(.)*6)+1+INT(RND(.)*6)+1
120 T(S)=T(S)+1
130 NEXT
200 PRINTCHR$(147)
210 PRINT"DICE    FREQUENCY    PERCENTAGE"
220 PRINT"-------------------------------"
230 PRINT:FORJ=2TO12:PRINTJ;TAB(9);T(J);TAB(21);T(J)/N*100;"%":NEXT
1100 PRINT:PRINT"PROCESS TIME:"TI/60

1000 times:

N=1000

and now 10000 times:

N=10000

How does this compare?

For gaming and the kinds of things you'd probably use your Commodore, these results are probably perfectly fine. Is it really random? It's good enough random.

Let's try this on a modern computer with Python, which is widely used in 2021 in scientific and data analysis.

1000 times single dice:

N=1000

and now 10000 single dice:

N=10000

1000 times two dice:

10000 times two dice:

It turns out that the Commodore seems to have fared pretty well. These are simple calculations for both systems. Even if the 2021 Macbook completed all of the tests in under 1 second, the results aren't that different.

Well done, Commodore.

And before you get all twisted up about this, it's just for fun. Nothing serious is implied here. We are just trying some practical uses of randomness in simple dice game applications.