Not More F'n Dice. Don't you Have Better Things to Do!!

As most of my post of late have been D&D related and my last post one of the major part of the game is simple rolling of dice.

Some of you might be old enough to remember this screen shot

Might and Magic 2 (C-64).jpg

Well if you where playing this on an IMB/XT all you had to do was go into DOS set the clock to 22:22:22 and then you would roll 18s for most of you ability scores?? Well it seems they where seeding the random generator with the time and that time got you the same 'good' results each time. Going back to the Apple ][+ days its randomizer was so bad you could easily see the patterns. The the Hi-Lo guessing game was one I remember transcribing from C64~Basic to AppleSoft. If the first number was 19 then next was 63 then 45.

So basically I was wondering it things are better these days.

Well I stole the code from Ovid's post, less the bug, and did a quick test for a D20 for 10,000 roles and got this


1 486 2.8%
2 513 2.60%
3 481 3.8%
4 490 2.0%
5 506 1.20%
6 507 1.40%
7 495 1.00%
8 507 1.40%
9 505 1.00%
10 523 4.60%
11 506 1.20%
12 485 3.00%
13 448 1.4%
14 503 0.6%
15 537 7.40%
16 516 3.20%
17 510 2.00%
18 514 2.80%
19 496 0.8%
20 472 5.6%

Well what does this mean. You will have to ask Sinan but what I can do is compare it to a similar test done with real dice

1    508    1.57%
2    564   11.35%
3    496    0.80%
4    532    6.02%
5    488    2.40%
6    492    1.60%
7    503    0.60%
8    580   13.79%
9    474    5.20%
10   555    9.91%
11   533    6.19
12   486    2.80%
13   463    7.40%
14   295   41.00%
15   491    1.80%
16   499    0.20%
17   443   11.40%
18   602   16.94%
19   522    4.21%
20   474    5.20%

So perhaps Perl dice are just too consistent??

I even took a little time to dumb it down to a coin toss a'la Kerrich

so here are a few results


10 8 -3
50 29 -4
100 52 -2
500 257 -7
1000 511 -11
5000 2542 -42
10000 5013 -66
100000 50283 -283
1000000 499908 92

and Kerrich's results

    10       4  -1
    50      25   0    
   100      44  -6
   500     255   5
  1000     502   2
  5000    2533  33
 10000    5067  67

Which is what we want to see, the % size of the error getting smaller as the number of tosses goes up but the absolute number increasing.

In the end what does this tell me?? Well there is no random error or at least one that is big enough to mean anything so we get near perfect dice rolls, The stats police might come after me for saying that though.

So one thing I did lean while doing this was that we have 'srand' in perl so if you ever needed the exact same set of random numbers just set that first and you will get the same results, which if you are doing some sort of testing it would be good to know your seed so you could repeat the results.

The funny thing I did was run my little tests with and without srand and let perl do its own thing. According to the doc it resets the seed each time it is called and I found that those numbers even less random error in them?

I set it to run 10000 times and only saw 10.5% as the highest deviation well withing the 85% most of the time in the 96% range.

So no need to worry about random rolls and patterns at least for my silly game

And just for a hoot I went to another site and tried the same test

1        474    5.20% 
2        515   -3.00%   
3        523   -4.60%  
4        506   -1.20%   
5        544   -8.80%   
6        499    0.20%    
7        485    3.00%    
8        487    2.60%    
9        493    1.40%   
10       485    3.00%   
11       520   -4.00%    
12       509   -1.80%    
13       499    0.20%   
14       528   -5.60%   
15       490    2.00%    
16       489    2.20%   
17       453    9.40%   
18       511   -2.20%   
19       512   -2.40%    
20       478    4.40%   

A little more random than perl funny.

2 Comments

Sinan has a better program he explains in Randomness and statistical concepts, since randomness isn't as easy as people think it is. :)

Leave a comment

About byterock

user-pic Long time Perl guy, a few CPAN mods allot of work on DBD::Oracle and a few YAPC presentations