FAQ
Why is an all-zero state forbidden?
For the xoshiro/xoroshiro families the all-zero state maps to itself: the generator would output only zeros forever. The two components of MRG32k3a and MRG63k3a have the same degeneracy (an all-zero component stays zero). Constructors, srand!, set_state! and Random.seed! all reject such a seed with an ArgumentError, and checkseed (checkseed63 for MRG63k3a) is the predicate they use, so you can test a seed yourself before handing it over. An integer seed never produces one. PCG and the counter-based generators have no invalid seed at all.
Are MRG integer outputs uniform over their full width?
No, for neither member of the family, though the two are not equally far from it. MRG32k3a natively produces ~32 bits per draw (z = p1 - p2 in [1, m1], m1 = 2^32 - 209), and wider integers are assembled from 16-bit chunks of that value, whose departure from uniformity is of order 2^-16 per chunk. MRG63k3a produces just under 63 bits (m1 = 2^63 - 6645) and assembles 32-bit chunks, at 2^-31 per chunk — the same construction, two orders of magnitude closer to uniform, and half as many steps per word.
Both are fine for indices, shuffling, flags and acceptance tests in simulations, and neither is exactly uniform over 2^64/2^128. Use a xoshiro variant, PCG or a counter-based generator when you need full-width raw words.
MRG32k3a or MRG63k3a?
Same author, same construction, same stream semantics; the difference is the word size the arithmetic is built for. Take MRG32k3a when you need the published, widely implemented parameter set — matching SSJ, RngStreams or another simulation package draw for draw — or when your run consumes floats almost exclusively. Take MRG63k3a when the draws are integers (its UInt64 costs two steps against four, and is far closer to uniform), or when you want the longer period (2^377 against 2^191) and the wider stream spacing that comes with it. Note that the stream and substream distances of MRG63k3a are this package's own choice, since L'Ecuyer published jump matrices for MRG32k3a only.
Why doesn't sample(v, k) work with RandomDataStreams generators?
sample comes from StatsBase.jl, not from the Random standard library — even Julia's own RNGs do not provide it without that package. Everything in Random.jl works with RandomDataStreams generators.
Where did srand, next_stream, short_jump, long_jump go?
They mutated their argument despite not ending in !; they are deprecated in favour of:
| Deprecated | Replacement |
|---|---|
srand(rng/gen, seed) | srand!(rng/gen, seed) |
next_stream(gen) | next_stream!(gen) |
short_jump(rng) | short_jump!(rng) |
long_jump(rng) | long_jump!(rng) |
The old names still work but emit a deprecation warning.
What does an integer seed mean?
The same thing for every generator: it is expanded through splitmix64 and folded into whatever that family accepts as a state or key, so T(12345), Gen(12345) and Random.seed!(T(), 12345) all agree. A value in the family's own representation — its seed vector, or a UInt128 for PCG — is taken as the state itself rather than hashed. See Streams & Substreams for the full table.
Can rand return 0 or 1?
No, for every generator and every floating-point type: draws lie in the open interval (0, 1), so -log(rand(rng)) or a quantile function applied to a draw is always finite. The price is one bit of resolution — 52 bits for a Float64, 23 for a Float32. In exchange the draws sit on a grid symmetric about 1/2, so 1 - u is exact and is itself a possible draw, which is what antithetic variates need. See Implementation Notes.
How do I run truly parallel simulations?
Take one stream per replication first, then parallelise over the replications: rngs = next_stream!(gen, nrep), and replication r draws from rngs[r]. Streams share no state, so this needs no synchronisation, and since the stream follows the replication rather than the thread, the results do not change with the number of threads.
Never call next_stream! on a shared generator object from inside a parallel loop. The generator rewrites the seed of the next stream on every call, so concurrent calls hand out overlapping streams and nothing reports it. For separate processes the seeds have to travel instead; both patterns are in Streams & Substreams.
Which generator should I pick?
See Generator Comparison. Short answer: Xoshiro256pp for general use, MRG32k3a for L'Ecuyer-compatible stream semantics (MRG63k3a for the same model with 64-bit arithmetic and a longer period), Philox4x64RNG when a draw must be addressable by index rather than reached by replaying a state.
What does "counter-based" buy me?
A counter-based generator computes its output as a keyed bijection of a counter, so the draw at position i of substream j of stream s is a function of (s, j, i). Nothing has to be replayed to reach it. That makes three things easy that are awkward otherwise: handing a stream identifier to a worker or a GPU kernel that has no generator object, jumping to an arbitrary position in constant time, and re-running one replicate of one stream without re-running anything else. The cost is speed: on scalar draws Philox and Threefry are several times slower than xoshiro, though filling arrays with rand! recovers much of the gap.
Philox or Threefry?
Philox4x64RNG is the faster of the two here and the one other libraries ship (Random123, cuRAND, TensorFlow, Intel MKL), so prefer it unless you have a reason not to. Threefry4x64RNG uses only additions, rotations and xors — no multiplication — which makes it a better fit for hardware without a fast wide multiply, and easier to audit.
Is it safe to use 1, 2, 3, ... as counter-based stream keys?
For Philox and Threefry, yes: ten (resp. twenty) rounds of encoding absorb the structure of consecutive small keys, as reported by Salmon et al. (2011) and discussed by L'Ecuyer et al. (2021, Sec. 3). This is not a property of counter-based generators in general — for the Squares generator, the key 0 gives an identically zero output — so a new family whose bijection is weak for structured keys must override stream_key with a schedule that hashes the seed. See Implementation Notes.
Can I reproduce cuRAND's Philox and MRG32k3a streams?
Yes, word for word. cuRAND's Philox4_32_10 is this package's PhiloxRNG: the seed is the key, its low 32 bits first, and cuRAND's subsequence is the package's substream — the high half of the counter — with the offset counting words into it:
rng = PhiloxRNG((s % UInt32, (s >> 32) % UInt32)) # cuRAND's seed s
for _ in 1:k
next_substream!(rng) # curand_init(s, k, 0)
end
rand(rng, UInt32) # the first curand(&state)The same holds for the host API, whose default ordering for Philox takes four words from each of 65,536 subsequences in turn. The test suite checks words that cuRAND generated itself, through both APIs, at subsequences up to 2^64 − 1.
What carries over is the 32-bit words, not what is built on them. cuRAND's curand_uniform returns x·2^-32 + 2^-33 in single precision, which is 1.0f0 for the largest word: its floats lie in (0, 1], this package's in (0, 1). Its normals are Box-Muller, this package's inversion. A pipeline that uses cuRAND's distributions therefore does not reproduce; one built on its words does, and a kernel of your own reaches any of them through philox4x32_10 and philox4x32_counter.
cuRAND's MRG32k3a is this package's MRG32k3a too. Seed 0 is L'Ecuyer's published state, the one MRG32k3a() starts from; another seed sets each component to 12345·x mod m, x being the seed's low half XOR 0x55555555 or its high half XOR 0xAAAAAAAA, as curand_init does. Its subsequence k is the package's substream k, k·2^76 steps in — next_substream! from MRG32k3a() for seed 0 — and the host API's default ordering takes output n from step n ÷ 81920 of subsequence n mod 81920. Here what carries over is the combination z in [1, m1]: cuRAND's doubles are z/m1, which is 1.0 for z = m1, the package's z/(m1 + 1), and its 32-bit integers are z scaled to 2^32 − 1, not the package's.
Can I reproduce a NumPy pipeline?
Partly. PCG64 here produces exactly the same raw 64-bit outputs as numpy.random.default_rng()'s bit generator for the same 128-bit state, and PCG64DXSM matches NumPy's PCG64DXSM. What does not transfer is how NumPy gets to that state: SeedSequence turns a user seed into the state through its own hashing, and that specification is not implemented here. So you can reproduce a NumPy stream if you carry the state across, not if you carry only the seed.
The distributions differ too. rand(rng) and numpy's random() consume the same bits but not necessarily in the same way, and randn draws by inversion where NumPy uses a Ziggurat. Only the raw generator output is guaranteed to agree.
Why can't I choose PCG's increment to get more streams?
Because it does not give independent streams. Every odd increment gives a full-period orbit, but for half of all pairs of odd increments the two orbits are the same state sequence shifted by a constant — an adversarial pair can be built whose outputs differ by an average of 2 bits out of 64. Most pairs are harmless, but no published criterion separates them, so the package fixes the increment and derives streams from jumps instead. The algebra is in the Implementation Notes. NumPy takes the same practical position: its recommended way to parallelise is SeedSequence.spawn(), not the increment.
Does this package run on the GPU?
For the counter-based generators, yes. With CUDA.jl loaded, rand! fills a CuArray{Float64}, CuArray{Float32} or CuArray{Float16} on the device, bit for bit what the same call gives on the CPU from the same position, and leaves the generator object — which stays on the host — where the CPU fill would. randn! and randexp! do the same for normal and exponential variates, which every generator draws by inversion: the GPU's values are the CPU's. The recurrence-based generators assign and navigate streams on the host only. See Streams & Substreams.
Why does randn use inversion rather than the Ziggurat?
Because the Ziggurat breaks what this package's streams are for. It takes a variable number of uniforms per normal, and builds a normal from different bits of one word, so reflecting a uniform does not negate a normal, two systems that share uniforms need not share normals, and a quasi-Monte Carlo point does not map to one normal per coordinate. Inversion, one uniform per normal through Φ⁻¹, has none of these problems, at a cost in speed. The comparison is tabulated in Normal variates and variance reduction.