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.
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.
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.
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.
This figure needs JavaScript.
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 streams
next_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 generator
next_substream!,reset_substream!andreset_stream!replay exactly the same uniforms: the basis of common random numbers. - Full position control
get_stateandsrand!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,randpermwork unchanged. - Normals by inversion
randnis Φ−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
| Family | Generators | Period | Streams |
|---|---|---|---|
| MRG32k3a | MRG32k3a | ≈ 2191 | streams of 2127 values, substreams of 276 (L’Ecuyer’s jump matrices) |
| MRG63k3a | MRG63k3a | ≈ 2377 | streams of 2250, substreams of 2150; 63 random bits per step |
| xoshiro / xoroshiro | 128, 256 and 512 bits of state, scramblers +, ** and ++ | 2128 to 2512 | Vigna’s jump polynomials |
| PCG | PCG64, PCG64DXSM | 2128 | closed-form LCG jumps, not the increment |
| Philox | 4×32-10, 4×64-10 | 2130 | distinct keys, counter jumps |
| Threefry | 4×64-20, 4×32-20 | 2130 | distinct 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
Documentation GitHub repository Guided tour (Jupyter notebook) Changelog
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).