What is a good random number generator for a game?

The other thread mentioned Marsaglia’s xorshf generator, but no one posted the code.

static unsigned long x=123456789, y=362436069, z=521288629;

unsigned long xorshf96(void) {          //period 2^96-1
unsigned long t;
    x ^= x << 16;
    x ^= x >> 5;
    x ^= x << 1;

   t = x;
   x = y;
   y = z;
   z = t ^ x ^ y;

  return z;
}

I’ve used this one all over the place. The only place it failed was when I was trying to produce random binary matrices. Past about 95×95 matrices, it starts generating too few or too many singular matrices (I forget which). It’s been shown that this generator is equivalent to a linear shift feedback register. But unless you are doing cryptography or serious monte carlo work, this generator rocks.

Leave a Comment