Dice frequency
By Michael Doornbos
- 3 minutes read - 523 wordsI’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:
And it gets slightly better at 10000 iterations:
Two dice
Two dice are also pretty easy to calculate. There’s a 1/36 chance of any of the possible combinations.
DICE | COMBINED | COMBINATIONS | PROBAILITY | % | |
---|---|---|---|---|---|
2 | 1+1 | 1/36 | 2.78% | ||
3 | 1+2 or 2+1 2/36 | 5.56% | |||
4 | 1+3 or 2+2 or 3+1 | 3/36 | 8.33% | ||
5 | 1+4 or 2+3 or 3+2 or 4+1 | 4/36 | 11.11% | ||
6 | 1+5 or 2+4 or 3+3 or 4+2 or 5+1 | 5/36 | 13.89% | ||
7 | 1+6 or 2+5 or 3+4 or 4+3 or 5+2 or 6+1 | 6/36 | 16.67% | ||
8 | 2+6 or 3+5 or 4+4 or 5+3 or 6+2 | 5/36 | 13.89% | ||
9 | 3+6 or 4+5 or 5+4 or 6+3 | 4/36 | 11.11% | ||
10 | 4+6 or 5+5 or 6+4 | 3/36 | 8.33% | ||
11 | 5+6 or 6+5 | 2/36 | 5.56% | ||
12 | 6+6 | 1/36 | 2.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:
and now 10000 times:
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:
and now 10000 single dice:
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.