state->x[0]; /* Perform a contrived 32-bit shift. */ state->x[3] = state->x[2]; state->x[2] = state->x[1]; state->x[1] = s; t ^= t << 11; t ^= t >> 8; return state->x[0] = t ^ s ^ (s >> 19); } Non-linear variations All xorshift generators fail some tests in the BigCrush test suite. This is true for all generators based on linear recurrences, such as the Mersenne Twister or WELL. However, it is easy to scramble the output of such generators to improve their quality. The scramblers known as and still leave weakness in the low bits, so they are intended for floating point use, as conversion to floating point discards the low bits. For general purpose, the scrambler (pronounced starstar) makes the LFSR generators pass in all bits. xorwow Marsaglia suggested scrambling the output by combining it with a simple additive counter modulo 232 (which he calls a "Weyl sequence" after Weyl's equidistribution theorem). This also increases the period by a factor of 232, to 2192−232: #include <stdint.h> struct xorwow_state { uint32_t x[5]; uint32_t counter; }; /* The state array must be initialized to not be all zero in the first four words */ uint32_t xorwow(struct xorwow_state *state) { /* Algorithm "xorwow" from p. 5 of Marsaglia, "Xorshift RNGs" */ uint32_t t = state->x[4]; uint32_t s = state->x[0]; /* Perform a contrived 32-bit shift. */ state->x[4] = state->x[3]; state->x[3] = state->x[2]; state->x[2] = state->x[1]; state->x[1] = s; t ^= t >> 2; t ^= t << 1; t ^= s ^ (s << 4); state->x[0] = t; state->counter += 362437; return t + state->counter; } This performs well, but fails a few tests in BigCrush. This generator is the default in Nvidia's CUDA toolkit. xorshift* An generator applies an invertible multiplication (modulo the word size) as a non-linear transformation to the output of an generator, as suggested by Marsaglia. All generators emit a sequence of values that is equidistributed in the maximum possible dimension (except that they will never output zero for 16 calls, i.e. 128 bytes, in a row). The following 64-bit generator has a maximal period of 264−1. #include <stdint.h> /* xorshift64s, variant A_1(12,25,27) with multiplier M_32 from line 3 of table 5 */ uint64_t xorshift64star(void) { static uint64_t x = 1; /* initial seed must be nonzero, don't use a static variable for the state if multithreaded */ x ^= x >> 12; x ^= x << 25;