logo
Kurashizu Blogwhere ideas flow
  • Home
  • News
  • Blog
  • About
  • Home
  • News
  • Blog
  • About

© 2026 Kurashizu. All rights reserved.

Admin·New Post·Service Status
Powered by Cloudflare·31.10.26
← Back to news

Xorshift Generators

Original article
alanzucconi.com|19 points|13 comments|by tobr|Aug 15, 2026

Xorshift Generators: A Deep Dive into Fast PRNGs

Definition: Xorshift is a class of pseudorandom number generators (PRNGs) discovered by George Marsaglia. These generators are based on a linear-feedback shift register (LFSR) logic, utilizing exclusively the XOR and bit-shift operations to produce a sequence of numbers.

1. The Core Mechanism

At its heart, a Xorshift generator transforms a state variable by shifting its bits and XORing the result back into the original state. This process creates a sequence of numbers that appears random but is entirely deterministic.

Mathematical Representation

The basic transition of a Xorshift generator can be expressed using the following LaTeX notation:

xn+1=xn⊕(xn≪a)x_{n+1} = x_n \oplus (x_n \ll a)xn+1​=xn​⊕(xn​≪a) xn+1=xn+1⊕(xn+1≫b)x_{n+1} = x_{n+1} \oplus (x_{n+1} \gg b)xn+1​=xn+1​⊕(xn+1​≫b) xn+1=xn+1⊕(xn+1≪c)x_{n+1} = x_{n+1} \oplus (x_{n+1} \ll c)xn+1​=xn+1​⊕(xn+1​≪c)

Where:

  • ⊕\oplus⊕ denotes the exclusive OR operation.
  • ≪\ll≪ and ≫\gg≫ denote left and right bit-shifts.
  • a,b,ca, b, ca,b,c are specific constants chosen to maximize the period of the generator.

Logic Flow

The following Mermaid diagram illustrates the iterative cycle of a standard Xorshift operation:


2. Implementation and Performance

Xorshift is prized for its extreme efficiency. Because it avoids expensive operations like multiplication or division (which are common in Linear Congruential Generators), it can be implemented in just a few CPU cycles.

Example Implementation (C++)

Below is a standard implementation of a 32-bit Xorshift generator:

#include <iostream>
#include <cstdint>

uint32_t xorshift32(uint32_t *state) {
    uint32_t x = *state;
    x ^= x << 13;
    x ^= x >> 17;
    x ^= x << 5;
    *state = x;
    return x;
}

int main() {
    uint32_t seed = 42; // Must be non-zero
    for(int i = 0; i < 5; i++) {
        std::cout << xorshift32(&seed) << "\n";
    }
    return 0;
}

Comparison with Other PRNGs

GeneratorSpeedPeriodMemory FootprintStatistical Quality
LCGFastShortVery LowPoor
Mersenne TwisterModerateMassiveHighExcellent
XorshiftBlazingLongLowGood

3. Nuances and Limitations

While powerful, Xorshift is not without its quirks. Developers must be aware of specific pitfalls to ensure the generator behaves as expected.

The Zero-State Trap

The most critical rule of Xorshift is that the state must never be zero.

  • If the state becomes 0, every subsequent operation (0⊕(0≪a)0 \oplus (0 \ll a)0⊕(0≪a)) will result in 0.
  • The generator will effectively "freeze," producing a constant stream of zeros.

Cryptographic Suitability

It is a common misconception that any fast PRNG is suitable for security. Xorshift is a secure replacement for /dev/urandom. Actually, Xorshift is not cryptographically secure. Because it is linear, an observer who sees a small portion of the output can use linear algebra to predict all future values.

Evolution of the Algorithm

Over time, the original Xorshift has evolved into more robust versions to pass stricter statistical tests (like BigCrush):

  1. Xorshift+: Adds the state to a shifted version of itself to break linearity.
  2. Xorshift*: Multiplies the result by a constant.
  3. Xoshiro / Xoroshiro: The modern gold standard, combining rotation (rot) with XOR and shifts.

4. Integration Checklist

When implementing a Xorshift-based system, ensure you have completed the following tasks:

  • Seed Validation: Ensure the initial seed is ≠0\neq 0=0.
  • Constant Verification: Use proven triplets (a,b,c)(a, b, c)(a,b,c) for the specific bit-width.
  • State Isolation: Use inline code or encapsulated classes to prevent external state mutation.
  • Statistical Testing: Run the output through a suite like TestU01.

PRNG Visualization Figure 1: A conceptual representation of a random walk generated by a PRNG.

Summary

Xorshift generators offer a compelling trade-off between computational speed and randomness quality. By utilizing simple bitwise logic, they provide a high-period sequence with minimal memory overhead, making them ideal for simulations, game development, and non-cryptographic applications.