Xorshift Generation

suggest change

A good, easy alternative to the flawed rand() function is xorshift, a class of pseudo-random number generators discovered by George Marsaglia. The xorshift generator is among the fastest non-cryptographically-secure random number generators. More information and other example implementations are available on the xorshift Wikipedia page.

The state is shared, so do not call xorshift128 from more than one thread at a time without external synchronization. An all-zero state stays zero, so the variables need nonzero seeds.

Example implementation

#include <stdint.h>

/* Marsaglia's sample seeds. Not all zero. */
uint32_t w = 88675123u, x = 123456789u, y = 362436069u, z = 521288629u;

uint32_t xorshift128(void)
{
    uint32_t t = x;
    t ^= t << 11U;
    t ^= t >> 8U;
    x = y; y = z; z = w;
    w ^= w >> 19U;
    w ^= t;
    return w;
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents