Level: Rookie

Joined: Fri. May 11, 2007

Posted by darsie:
Sat. Nov 3, 2012 - 05:28 PM
uint8_t rnd(void) {
static uint8_t s=0xaa,a=0;
s^=s<<3;
s^=s>>5;
s^=a++>>2;
return s;
}

Tiny and fast 8 bit pseudo random number generator. Period is generally 55552 bytes. You can seed by changing s and/or a, but about 16% of the seeds give much shorter periods and reduced randomness.

Randomness analysis with ent:

bernhard@b:/data/home/bernhard/src/prng$ ./xorshift 0xaa 0 3 5|head -c 1000000|ent

Entropy = 7.999790 bits per byte.

Optimum compression would reduce the size

of this 1000000 byte file by 0 percent.

Chi square distribution for 1000000 samples is 290.74, and randomly

would exceed this value 10.00 percent of the times.

Arithmetic mean value of data bytes is 127.4850 (127.5 = random).

Monte Carlo value for Pi is 3.145164581 (error 0.11 percent).

Serial correlation coefficient is -0.001617 (totally uncorrelated = 0.0).

## Tags:

Last Edited: Sat. Nov 3, 2012 - 06:36 PM