PCG
Permuted congruential generators: LCG state with a permutation on output
Overview
PCG is a family of pseudorandom number generators that combine a linear congruential generator's state transition with an output permutation that hides its statistical weaknesses. They are fast, compact and statistically strong, and PCG64 is the default bit generator in NumPy, but they are not cryptographic.
A plain linear congruential generator (LCG) computes state = a*state + c modulo a power of two; its low bits have short periods and its output is easy to predict. PCG keeps the LCG state transition but outputs a permuted function of the state, such as a random rotation or an xorshift followed by a multiply, selected by the state's high bits. This produces output that passes demanding statistical test suites while keeping a tiny state.
Four variants are present. PCG32 uses a 64-bit state and produces 32-bit outputs. PCG64 uses a 128-bit LCG with the XSL-RR output function. PCG64-DXSM uses a stronger double-xorshift-multiply output function introduced later to address correlations between streams. PCG64-MCG uses a multiplicative congruential generator (c = 0), which is slightly faster but has a shorter period. Except for the MCG form, the variants support multiple independent streams selected by the increment.
History
2014–2014PCG published by Melissa E. O'Neill
How it works
- Advance the state: state = a*state + c mod 2^k (c = 0 for MCG).
- Apply the output permutation to the old or new state to produce the result.
- Select independent streams by choosing different odd increments c.
- Jump ahead in logarithmic time using LCG arithmetic.
Security notes
Not cryptographically secure: the internal state can be recovered from outputs with modest computation, making future outputs predictable. Do not use it for keys, nonces, session IDs or tokens; use the OS RNG or a DRBG.
Typical uses
Variants in NextSSL
4| Algorithm | Tier | Sizes (bytes) | Standards | API |
|---|---|---|---|---|
| PCG32 pcg32 |
unverified | out 1–268435456 · seed 16 | PCG32 (O'Neill 2014), NOT cryptographically secure | root |
| PCG64 pcg64 |
unverified | out 1–268435456 · seed 32 | PCG64 (O'Neill 2014), NOT cryptographically secure | root |
| PCG64-Dxsm pcg64-dxsm |
unverified | out 1–268435456 · seed 32 | PCG64 DXSM (O'Neill 2019), NOT cryptographically secure | root |
| PCG64-MCG pcg64-mcg |
unverified | out 1–268435456 · seed 16 | PCG64 MCG (O'Neill 2014), NOT cryptographically secure | root |
Frequently asked questions
Which PCG variants does NextSSL include?
NextSSL registers 4 PCG variants: pcg32, pcg64, pcg64-dxsm, pcg64-mcg.
Other non-cryptographic prngs
All 26Related
The classic long-period generator behind many language defaults, not for security
SplitMix64Counter-based 64-bit generator, widely used to seed other generators
xoshiro / xoroshiroFast linear generators from Blackman and Vigna for simulation, not for secrets
Operating system RNGThe kernel's cryptographically secure random number generator, the right default