Pseudo-random number generators

Pseudo-random number generators are at the heart of simulation and stochastic optimization. Their sequences look random yet can be reproduced at will: a model can be validated, systems compared with common random numbers, and computations spread over many processors without their draws overlapping.

Slides

PDF · 57 slides

“A short tutorial on random number generation”: uniform generators (LCG, MRG, generators over F₂, PCG, Philox), streams and substreams, non-uniform variates, multivariate distributions and copulas.

Open the PDF

Web tutorial

Interactive figures · Julia examples

The same path, written out in full, with figures to play with: the lattice of an LCG, a shift register, Philox, the streams of MRG32k3a, inversion, acceptance-rejection and the Gaussian copula.

Read the tutorial

RandomDataStreams.jl

Julia package · version 0.4.1 · MIT licence

Generators with disjoint streams and substreams for simulation and variance reduction: MRG32k3a, MRG63k3a, xoshiro, PCG, Philox and Threefry, behind one interface.

Overview GitHub

RandomDataStreams.jl

RandomDataStreams.jl provides pseudo-random number generators built for variance reduction. Each generator cuts its period into streams that never overlap, themselves cut into substreams, following the architecture of L’Ecuyer, Simard, Chen and Kelton (2002): one stream per replication or per processor, one substream per scenario, and every simulated configuration replays exactly the same uniforms.

By Jean Laprés-Chartrand and Fabian Bastin · version 0.4.1 (October 2026) · Julia 1.9 or later · MIT licence · a single dependency, the standard library Random.

This figure needs JavaScript.

The first stream of MRG32k3aGen(), as the package hands it out: the buttons call RandomDataStreams functions, and the log shows the numbers they return in Julia. After reset_substream!, rand gives the same values again; next_stream! jumps 2127 values ahead.
  • Disjoint streamsnext_stream!(gen) hands out a stream that overlaps no other; next_stream!(gen, n) hands out n at once, one per replication, ready for threads.
  • Substreams for every generatornext_substream!, reset_substream! and reset_stream! replay exactly the same uniforms: the basis of common random numbers.
  • Full position controlget_state and srand! save and restore a stream; advance_state!(rng, e, c) jumps 2e + c steps, forward or backward.
  • The standard Julia APIEvery stream is an AbstractRNG: rand, rand!, randn, randexp, shuffle, randperm work unchanged.
  • Normals by inversionrandn is Φ−1(u), one uniform per variate, rather than the Ziggurat: antithetic variates, common random numbers and randomized quasi-Monte Carlo carry over from the uniforms to the normals.
  • Checked against referencesxoshiro byte for byte against the original C code, Philox and Threefry against Salmon et al.’s test vectors, PCG64 against NumPy; TestU01 batteries. With CUDA.jl, the counter-based generators fill GPU arrays.

The generators

FamilyGeneratorsPeriodStreams
MRG32k3aMRG32k3a≈ 2191streams of 2127 values, substreams of 276 (L’Ecuyer’s jump matrices)
MRG63k3aMRG63k3a≈ 2377streams of 2250, substreams of 2150; 63 random bits per step
xoshiro / xoroshiro128, 256 and 512 bits of state, scramblers +, ** and ++2128 to 2512Vigna’s jump polynomials
PCGPCG64, PCG64DXSM2128closed-form LCG jumps, not the increment
Philox4×32-10, 4×64-102130distinct keys, counter jumps
Threefry4×64-20, 4×32-202130distinct keys, counter jumps

An example: common random numbers

Two inventory levels, s = 50 and s = 60, compared over 1,000 replications of 30 days with normal demand. With common random numbers, both levels face the same demand, replayed from the same substream:

using RandomDataStreams, Statistics

function cost(s, rng)                     # cost of 30 days of inventory
    total, stock = 0.0, Float64(s)
    for _ in 1:30
        stock = min(stock + 40, 200)
        d = max(0.0, 100 + 20randn(rng))  # the day's demand
        sold = min(stock, d)
        total += 5(d - sold) + 0.1max(0, stock - sold - 10)
        stock -= sold
    end
    total
end

gen = MRG32k3aGen()
demand = next_stream!(gen)
crn = map(1:1000) do _
    c50 = cost(50, demand)
    reset_substream!(demand)              # the same demand for s = 60
    c60 = cost(60, demand)
    next_substream!(demand)               # next replication
    c60 - c50
end

d50, d60 = next_stream!(gen, 2)           # without common numbers: two streams
ind = [cost(60, d60) - cost(50, d50) for _ in 1:1000]

var(crn), var(ind)                        # (0.144…, 645753.5…)

The variance of the difference falls from 645,754 with independent streams to 0.14 with common random numbers, about 4.5 million times smaller.

Installation

The package is registered in Julia’s General registry:

using Pkg
Pkg.add("RandomDataStreams")   # or, at the REPL: ] add RandomDataStreams

References

  • P. L’Ecuyer, R. Simard, E. J. Chen and W. D. Kelton (2002), “An Object-Oriented Random-Number Package with Many Long Streams and Substreams”, Operations Research, 50(6).
  • P. L’Ecuyer, O. Nadeau-Chamard, Y.-F. Chen and J. Lebar (2021), “Multiple Streams with Recurrence-Based, Counter-Based, and Splittable Random Number Generators”, Winter Simulation Conference.
  • J. K. Salmon, M. A. Moraes, R. O. Dror and D. E. Shaw (2011), “Parallel Random Numbers: As Easy as 1, 2, 3”, SC ’11.
  • D. Blackman and S. Vigna (2021), “Scrambled Linear Pseudorandom Number Generators”, ACM Transactions on Mathematical Software, 47(4).