[MUD-Dev] [DGN] The psychology of random numbers

Hans-Henrik Staerfeldt hhs at cbs.dtu.dk
Fri Jan 23 10:23:45 CET 2004


On Wednesday 21 January 2004 02:44, Dave Bacher wrote:

> The C PRNG can be made worse by using modulus to scale results.
> You can only properly scale results with a multiply followed by a
> divide, assuming integer math.  Any other technique, particularly
> modulus, skews the results.  Lets say the maximum number the LCG
> can generate is 32767.  If you modulus that by 100, you'll see
> that 68 through 99 occur less frequently than 0 through 67.  If
> you modulus by 1000, 768 through 999 occur less frequently, and if
> you modulus by 10000, 2868 through 10000 occur less frequently.

This is an interresting and very useful piece of knowledge! Usually,
your standard I wonder though, if use the usual C PRNG are used, how
many rolls do you have to make before you can determine this skew
with any kind of statistical significance, using the usual C PRNG's,
let alone notice this by playing the game?

So i made a little program to test some of this;

  #include <stdlib.h>
  #include <stdio.h>

  #define size 20
  #define rolls 100000000

  long long int count[size];

  int main() {
    int i;
    int d;
    int e=rolls/size;

    for (i=0;i<size;i++) count[i]=0;
    for (i=0;i<rolls;i++) {
      count[rand()%size]++;
    }
    for (i=0;i<size;i++) {
      d=count[i]-e;
      printf("%3d: %6d\t%.6f\n",i,d,100.0*d/e);
    }
    return 0;
  }

Results were;

    0:  -1966     -0.039320
    1:   -719     -0.014380
    2:   1900     0.038000
    3:   2124     0.042480
    4:     46     0.000920
    5:   1392     0.027840
    6:   3128     0.062560
    7:   2637     0.052740
    8:   1517     0.030340
    9:   -757     -0.015140
   10:  -1387     -0.027740
   11:  -3359     -0.067180
   12:  -1681     -0.033620
   13:   2176     0.043520
   14:  -1346     -0.026920
   15:  -3723     -0.074460
   16:   2109     0.042180
   17:  -2032     -0.040640
   18:  -2328     -0.046560
   19:   2269     0.045380

As you can see, by rolling 100 million times on a d20 (well a d20-1,
not important), the modulus count of a given dice varies at most
0.067180% from the expected for the 11. I know, i should do this for
several seeds, but i guess someone can contradict me if my numbers
were just a fluke.

Now, such a small skew can be extremely difficult to detect if you
don't keep an extremely accurate track of all your rolls. I
seriously doubt that any player will yell 'Hey why the f*** don't I
ever roll an 11?'.

The old 'random()' gives comparable results (at most: 10: -4539
-0.090780%) Using 'lrand48()' is also comparable; (at most: 17: 5026
0.100520%)

Anyway, though a few numbers on the table might be
enlightening... :-)

--
--Hans-Henrik Stærfeldt
_______________________________________________
MUD-Dev mailing list
MUD-Dev at kanga.nu
https://www.kanga.nu/lists/listinfo/mud-dev



More information about the mud-dev-archive mailing list