Getting Started

RandomDataStreams.jl provides pseudo-random number generators with support for non-overlapping streams and substreams, following the stream/substream model of L'Ecuyer et al. (2002).

Installation

using Pkg
Pkg.add("RandomDataStreams")

or, from a local clone:

using Pkg
Pkg.develop(path = "path/to/RandomDataStreams.jl")

Requirements: Julia ≥ 1.9. The only dependency is the standard library Random; CUDA.jl, if loaded, enables GPU fills for the counter-based generators (see Streams & Substreams).

Choosing a generator

RandomDataStreams.jl implements the full xoshiro/xoroshiro family of Blackman & Vigna, L'Ecuyer's MRG32k3a and MRG63k3a, O'Neill's PCG (the default bit generator of NumPy), and the Philox and Threefry CBRNGs. All variants of a xoshiro family share the same linear transition and jump constants; only the output scrambler differs.

TypeFamilyStatePeriodScrambler
PhiloxRNGPhilox4x32-10128-bit counter2^130Feistel-like network
Philox4x64RNGPhilox4x64-10128-bit counter2^130Feistel-like network
Threefry4x64RNGThreefry4x64-20128-bit counter2^130Threefish rounds (add/rot/xor)
Threefry4x32RNGThreefry4x32-20128-bit counter2^130Threefish rounds (add/rot/xor)
Xoroshiro128pxoroshiro128128 bits2^128 − 1s0 + s1
Xoroshiro128ssxoroshiro128128 bits2^128 − 1high bits, **
Xoroshiro128ppxoroshiro128128 bits2^128 − 1high bits, ++
Xoshiro256pxoshiro256256 bits2^256 − 1s0 + s3
Xoshiro256ssxoshiro256256 bits2^256 − 1high bits, **
Xoshiro256ppxoshiro256256 bits2^256 − 1high bits, ++
Xoshiro512pxoshiro512512 bits2^512 − 1s0 + s2
Xoshiro512ssxoshiro512512 bits2^512 − 1high bits, **
Xoshiro512ppxoshiro512512 bits2^512 − 1high bits, ++
MRG32k3acombined MRG (L'Ecuyer)192 bits≈ 2^191native Float64, 32-bit step
MRG63k3acombined MRG (L'Ecuyer)384 bits≈ 2^377native Float64, 63-bit step
PCG64PCG (O'Neill)128 bits2^128XSL-RR permutation
PCG64DXSMPCG (O'Neill)128 bits2^128DXSM permutation

See Generator Comparison for guidance and benchmarks.

Seeding: one rule for every generator

Whatever generator you pick, there are three ways to seed it, and they mean the same thing in every family:

MRG32k3aGen()             # the package default seed, 12345
MRG32k3aGen(20260830)     # an integer seed
MRG32k3aGen([1,2,3,4,5,6])# the family's own seed vector

Xoshiro256ppGen(20260830) # exactly the same three forms
PCG64Gen(20260830)
PhiloxGen(20260830)

An integer is a seed: it is expanded through splitmix64 and folded into whatever the family accepts, so T(20260830) is equivalent to Random.seed!(T(), 20260830) everywhere. A value in the family's own representation — its seed vector, or a UInt128 for PCG — is taken as the state or key itself, not hashed.

The same three forms work on the stream types directly (MRG32k3a(20260830), Xoshiro256pp(20260830), ...). See Streams & Substreams for the full interface table.

First numbers

using RandomDataStreams

rng = MRG32k3a()      # default seed [12345, ..., 12345]
rand(rng)             # 0.12701112204657714
rand(rng)             # 0.3185275653967945
x = Xoshiro256p(UInt64[1, 2, 3, 4])
rand(x)               # Float64 in (0, 1)

Supported output types

MRG32k3a

  • Float64 (native), plus Float32, Float16 via conversion
  • UInt8, UInt16, UInt32, UInt64, UInt128
  • Int8, Int16, Int32, Int64, Int128 (same bit patterns)
  • Bool
  • Ranges: rand(rng, 1:10) works through the standard Random machinery

Note: integer outputs of MRG32k3a are built from its native ~32-bit resolution, and MRG63k3a's from its ~63-bit one; they are not uniform over their full width. Use them for indices, choices and flags rather than cryptography.

Xoshiro256p

  • Float64 (native path), Float32, Float16, all in the open interval (0, 1) — see Implementation Notes
  • UInt64
  • Ranges: rand(rng, 1:10) works through the standard Random machinery

Reproducibility

Every generator accepts an explicit seed:

rng = MRG32k3a([42, 1, 2, 3, 4, 5])
x    = Xoshiro256p(UInt64[0xdead, 0xbeef, 0xcafe, 0xbabe])

Seeds are validated by checkseed, or checkseed63 for MRG63k3a: a valid seed of either has 6 non-negative values; the first three must be < m1 and not all zero, the last three < m2 and not all zero.

The standard Julia interface also works with every generator:

Random.seed!(rng, 42)          # integer seed (splitmix64 expansion)
Random.seed!(rng, [7,7,7,8,8,8])   # explicit MRG32k3a seed (validated)

Full Random API

RandomDataStreams generators are drop-in substitutes for Julia's built-in RNGs: anywhere a function accepts an AbstractRNG, you can pass an RandomDataStreams generator and keep your stream/substream control.

using RandomDataStreams, Random

rng = next_stream!(MRG32k3aGen())     # any RandomDataStreams generator works here

rand(rng, 5)                          # Vector{Float64}, 5 draws
A = rand(rng, Float64, 2, 3)          # 2x3 matrix
z = randn(rng)                        # standard normal, by inversion
e = randexp(rng)                      # exponential, by inversion
v = shuffle(rng, collect(1:8))        # shuffled copy
p = randperm(rng, 6)                  # random permutation
buf = Vector{Float64}(undef, 3)
rand!(rng, buf)                       # fill in place
Random.randstring(rng, 10)            # random string

Seeding through the standard interface makes results reproducible:

Random.seed!(rng, 42)
u1 = [rand(rng) for _ in 1:3]
Random.seed!(rng, 42)
u2 = [rand(rng) for _ in 1:3]
u1 == u2                              # true

Only sample requires StatsBase.jl and is out of scope.

Next steps