Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Pseudo-random number generators

ProblemProduce a long, fast, reproducible sequence of 64-bit integers (and uniform numbers in ) that passes statistical tests of randomness
Devised byJohn von Neumann (1946), middle-square; Derrick Lehmer (1949), linear congruential generators; George Marsaglia (2003), xorshift; Guy Steele, Doug Lea and Christine Flood (2014), SplitMix; David Blackman and Sebastiano Vigna (2018), xoshiro256++
AccuracyPeriod ; passes the BigCrush and PractRand test batteries; 53 random bits per floating-point number
CostAbout 2 ns per 64-bit number; 32 bytes of state
Read firstPreliminaries: probability
APIUniformGenerator, UniformRNG, Xoshiro256PlusPlus, SplitMix64, Histogram

Overview

A pseudo-random number generator (PRNG) is a deterministic state machine: a state, a function that advances it, and a function that outputs a number from it. A good one has a period far longer than any computation, outputs that pass every practical statistical test, and is fast. Because it is deterministic, the same seed reproduces the same sequence, which makes simulations debuggable and repeatable.

The library uses xoshiro256++ (the UniformRNG alias): 256 bits of state advanced by shifts, rotations and exclusive-ors, scrambled by an addition and a rotation. It is seeded through SplitMix64, a simple generator that spreads any 64-bit seed over the larger state. Neither is cryptographically secure: an observer can reconstruct the state from a few outputs. For keys and tokens, use an operating-system source.

Where it is used

A good generator is invisible; a bad one invalidates a simulation.

  • Scientific simulation. Monte Carlo codes need long, reproducible streams that pass statistical tests.
  • Reproducibility. A seeded generator lets a collaborator regenerate your experiment exactly.
  • Parallel computing. Independent streams for many processors.
  • Games and security. Games use fast generators; cryptography requires special ones, which these are not.

History

John von Neumann’s middle-square method (1946), squaring a number and keeping its middle digits, was quickly found to fall into short cycles; he remarked that “anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin”. Derrick Lehmer’s linear congruential generators (1949), , dominated for decades, despite infamous failures such as IBM’s RANDU, whose consecutive triples fall on 15 planes. Makoto Matsumoto and Takuji Nishimura’s Mersenne Twister (1998) brought enormous periods and became the default of many languages. George Marsaglia (2003) introduced fast xorshift generators, and Pierre L’Ecuyer and Richard Simard’s TestU01 (2007) set the standard of statistical testing. David Blackman and Sebastiano Vigna (2018) combined xorshift-style linear engines with nonlinear scramblers in the xoshiro and xoroshiro families, small, fast and statistically excellent.

The idea

xoshiro256++

A pseudo-random number generator has three parts: a state (a block of memory), an update that maps each state to the next, and an output function that turns the state into a number. Everything it produces is determined by the initial state, so the “randomness” is a matter of the outputs being statistically indistinguishable from random ones. Since there are finitely many states, the sequence must eventually repeat; the length of the cycle is the period.

The state and its update. In xoshiro256++ the state is four 64-bit words , 256 bits in all. The update uses only shifts, rotations and exclusive-ors, which are fast on every processor. Such an update is linear over : regard the state as a vector of 256 bits and do arithmetic modulo 2 (where addition is exclusive-or), and the update is multiplication by a fixed matrix of zeros and ones.

Linearity is what allows the period to be proved instead of hoped for. The matrix has been chosen so that its characteristic polynomial is primitive, and that guarantees that every non-zero state lies on one single cycle of length , the longest possible. The all-zero state is the one exception: it maps to itself, so it must never be used.

The output function. Linearity is also a weakness. If the output were simply a word of the state, its bits would satisfy linear relations that statistical tests can detect, the lowest bits especially. The output is therefore passed through a nonlinear scrambler. Each step returns

where the additions are ordinary 64-bit integer additions (whose carries are not linear over ) and rotl rotates the bits left. This is the “++” of the name.

From integers to floating point. A uniform number in is made from the top 53 bits of the output, . A double holds 53 significant bits, so every value produced is an exact multiple of and all of them are equally likely. The result can be exactly 0 but never 1, which matters for samplers that take logarithms: they use instead of .

Seeding with SplitMix64

A user supplies a seed of 64 bits, but the generator needs 256 bits of state, and they should look unrelated to each other even when the seeds are simple numbers such as 1, 2, 3. A second, simpler generator does the expansion. SplitMix64 keeps a counter, adds the constant (with the golden ratio) to it at each call, and passes the result through a strong mixing function, two rounds of multiplying and xor-shifting, in which every input bit affects every output bit. Its outputs for consecutive counters are effectively independent, and four of them fill the state.

The library’s version also skips the value 0, so that a seed can never produce the forbidden all-zero state. That check is recursive, which keeps it from being inlined, and makes it slower than xoshiro in the table below; it is used only for seeding.

Independent streams

A parallel computation needs one stream of random numbers per worker, and the streams must not overlap, or the workers would be using the same “random” numbers. Linearity provides a guarantee. Advancing the state by one step is multiplication by the matrix , so advancing by steps is multiplication by , another fixed linear map. The jump function applies it in one call, using a precomputed polynomial. Calling jump repeatedly yields starting states that are steps apart: since the period is about , there is room for such streams, each numbers long, and they provably do not overlap.

The alternative of seeding each worker with a different seed works in practice, because the period is so vast that two random starting points are very unlikely to be close, but it carries no guarantee.

Histograms

Histogram counts samples in equal bins over , the quickest visual check of a sampler, and the basis of chi-square goodness-of-fit tests. Bins start at bin_min, and the upper edge belongs to the last bin.

Speed

Table 9.3. Time per sample of the generators and samplers.

operationtime per sample
xoshiro256++ next_u640.6 ns
xoshiro256++ next_f640.7 ns
SplitMix64 next_u641.7 ns
exponential (inversion)3.5 ns
normal (Box-Muller, one pair per two samples)11.9 ns
gamma, shape 2.5 (Marsaglia-Tsang)30.1 ns
Poisson, 22.0 ns

Algorithm

next_u64, one step of xoshiro256++, on the state words :

  1. Output. Compute result = rotl(s0 + s3, 23) + s0, with wrapping 64-bit additions, from the state as it stands.
  2. Update the state, in this order: t = s1 << 17; s2 ^= s0; s3 ^= s1; s1 ^= s2; s0 ^= s3; s2 ^= t; s3 = rotl(s3, 45).
  3. Return result.

The whole step is about ten machine instructions, with no multiplication and no branch.

next_f64: take next_u64, shift it right by 11 bits to keep the top 53, and multiply by .

Seeding from a 64-bit seed: run SplitMix64 four times from the seed, and use the four outputs as .

jump: start a new state at zero. For each bit of a fixed 256-bit constant, in order: if the bit is 1, exclusive-or the current state into the new state; then advance the generator one step. Finally replace the state by the new state. The constant encodes the polynomial that makes this equivalent to steps.

Implementation

    /// Generate next random u64.
    fn next_u64(&mut self) -> u64 {
        let result = Self::rotl(self.s[0].wrapping_add(self.s[3]), 23)
            .wrapping_add(self.s[0]);

        let t = self.s[1] << 17;

        self.s[2] ^= self.s[0];
        self.s[3] ^= self.s[1];
        self.s[1] ^= self.s[2];
        self.s[0] ^= self.s[3];

        self.s[2] ^= t;
        self.s[3] = Self::rotl(self.s[3], 45);

        result
    }
    pub fn next_u64(&mut self) -> u64 {
        let mut z = {
            self.state = self.state.wrapping_add(0x9E3779B97F4A7C15);
            self.state
        };

        z = (z ^ (z >> 30)).wrapping_mul(0xBF58476D1CE4E5B9);
        z = (z ^ (z >> 27)).wrapping_mul(0x94D049BB133111EB);
        let result = z ^ (z >> 31);
        if result == 0{
            self.next_u64()
        }else{
            result
        }
    }
  • UniformRNG::from_seed(u64) seeds through SplitMix64; from_array_seed takes the state directly (rejecting all zeros); from_random_seed uses the clock, convenient but not reproducible.

In practice

//! Uniform pseudo-random generators: seeding, independent streams and a histogram check.
//!
//! Run with `cargo run -p lib_jsl_random --example random_generators`.

use lib_jsl_random::histogram::Histogram;
use lib_jsl_random::uniform_generator::{UniformGenerator, UniformRNG};

fn main() {
    // The same seed gives the same sequence: simulations are reproducible.
    let mut a = UniformRNG::from_seed(42);
    let mut b = UniformRNG::from_seed(42);
    println!("same seed, same numbers: {}", (0..1000).all(|_| a.next_u64() == b.next_u64()));

    // Independent streams for parallel workers: jump each copy 2¹²⁸ steps ahead of the last.
    let mut streams = vec![UniformRNG::from_seed(7)];
    for _ in 1..4 {
        let mut next = streams.last().unwrap().clone();
        next.jump();
        streams.push(next);
    }
    let firsts: Vec<f64> = streams.iter_mut().map(|s| s.next_f64()).collect();
    println!("first value of each of 4 streams: {firsts:.4?}");

    // A histogram of 100 000 uniforms: each of 10 bins should hold about 10%.
    let mut h = Histogram::new(0.0, 1.0, 10);
    let mut rng = UniformRNG::from_seed(1);
    for _ in 0..100_000 {
        h.count(rng.next_f64()).unwrap();
    }
    let fractions = h.get_counts_normalized().unwrap();
    println!("bin fractions: {fractions:.4?}");
}

Strengths

  • Fast, small, statistically excellent, with a period of and cheap independent streams.

Weaknesses

  • Not cryptographically secure.
  • Floating-point numbers have 53 random bits, so values below never occur except 0.

Exercises

  1. Show that a linear congruential generator with modulus has a lowest bit that alternates.
  2. Find the 15 planes of RANDU, , by showing .
  3. Estimate by the fraction of uniform points in the unit quarter disc, and the error, for .
  4. Run a chi-square test on 10 bins of 100 000 uniforms using Histogram, and on the low 3 bits of next_u64.
  5. Verify experimentally that streams separated by jump show no correlation over values.
Hints and answers
  1. If and are odd, , so the lowest bit alternates; the second bit has period 4, and so on.
  2. , so . Then . Consequently : every triple lies on one of at most 15 planes.
  3. The standard error of is about , so the error falls from about at to about at .
  4. For a good generator, the chi-square statistic has mean equal to the degrees of freedom (9 for 10 bins) and should be above the 5% critical value (16.9) only about one time in twenty; repeat with several seeds.
  5. Compare the sample correlation of the two streams at several lags; for values expect values of order .

Further reading

  • D. Blackman and S. Vigna, “Scrambled linear pseudorandom number generators”, ACM Transactions on Mathematical Software 47 (2021) 36:1–36:32.
  • G. L. Steele, D. Lea and C. H. Flood, “Fast splittable pseudorandom number generators”, OOPSLA (2014) 453–472.
  • P. L’Ecuyer and R. Simard, “TestU01: A C library for empirical testing of random number generators”, ACM Transactions on Mathematical Software 33 (2007) 22:1–22:40.
  • D. E. Knuth, The Art of Computer Programming, Volume 2: Seminumerical Algorithms, 3rd ed., Addison-Wesley, 1997, chapter 3.