HeadlinesBriefing favicon HeadlinesBriefing.com

Xorshift Generators – Fast Bitwise PRNGs

Hacker News •
×

Xorshift generators are simple PRNGs based on XOR and shift operations. They were introduced by George Marsaglia in 2003 as a lightweight alternative to Mersenne Twister. The algorithm uses a few bitwise operations, resulting in a fast and compact implementation.

The core of a xorshift is a state variable that is updated by successively applying XOR and left/right shifts. A typical 32‑bit generator uses three shifts, while a 64‑bit version uses four. The period of a properly parameterised 32‑bit generator can reach 2^32‑1, and a 64‑bit version can achieve 2^64‑1.

Because of its minimal overhead, the generator is popular in performance‑critical code and educational demonstrations. However, the state transition is linear over GF(2), so it is unsuitable for cryptographic purposes. Randomness tests show a slight bias in the low‑order bits, which can be mitigated by tempering or combining with other generators.

Many libraries expose xorshift engines, for example the C++ standard library’s `std::xorshift` and Python’s `random` module offers a 64‑bit variant. Intel’s implementation of a vectorised xorshift is used in high‑throughput simulations, and the algorithm remains a staple for quick pseudo‑random number generation in embedded systems.