Toy PRNG: XorShift64*

medium · cryptography, randomness, prng, bitwise

Toy PRNG: XorShift64*

This task implements a toy pseudorandom number generator to illustrate how deterministic generators work and why statistical randomness is not the same as cryptographic unpredictability.

Implement the XorShift64* step:

x ^= x >> 12
x ^= x << 25
x ^= x >> 27
return x * 2685821657736338717

The state is updated to x before the multiplication.

Important: This generator is not cryptographically secure. It is only a learning tool.

Function signatures

type XorShift64Star struct {
    State uint64
}

func NewXorShift64Star(seed uint64) XorShift64Star
func (x *XorShift64Star) Next() uint64

Behavior

  • If the seed is 0, replace it with 0x9E3779B97F4A7C15 (a non-zero odd constant) to avoid the all-zero state.
  • Next() must update the internal state and return the next output.

Example

With seed 1, the first output is:

0x47e4ce4b896cdd1d

Constraints

  • Seed is any uint64 value.
  • Use 64-bit overflow semantics (standard uint64 in Go).
Run tests to see results
No issues detected