Skip to content
Systems programming for what’s next.
— Vex

rand — explicit, fast random streams ​

rand provides independently owned Rng values based on xoshiro256**. It is intended for simulation, randomized algorithms, games, sampling and tests. It has no hidden global generator.

Rng4 is the explicit parallel companion: four canonical jump-separated streams evaluated with Vex fixed-array SIMD operators. Scalar Rng ordering is never silently changed for throughput.

vex
import { Rng } from "rand"

let! replayable = Rng.withSeed(42)
let! independentlySeeded = Rng.init()

let word = replayable.nextU64()
let ratio = replayable.nextFloat()        // [0.0, 1.0)
let index = replayable.nextRange(0, 100)  // [0, 100)

Rng.init() seeds from the target's fail-closed Crypto.secureRand() capability. xoshiro output remains non-cryptographic: use the crypto API itself for keys, tokens, nonces and secrets. Randomness alone does not guarantee a unique AEAD nonce; see the AEAD nonce lifecycle.

Core API ​

APIResult
nextU64, nextI64, nextU32, nextI32full-width integer output
nextFloat, nextFloat32uniform semi-open floating output
nextBelow(upper)unbiased u64 in [0, upper)
nextRange, nextRangeU64, nextRangeFuniform semi-open ranges; nextRangeF rejects non-finite bounds deterministically
choose, chooseSpanborrowed selection without cloning
shuffle, unsafe shuffleSpanownership-neutral Fisher–Yates shuffle
fillBytes(&Vec<u8>!)safe deterministic pseudorandom fill
unsafe fillBytes(&Span<u8>!)raw-backed fill with caller-proven exclusivity
jump, longJumpadvance by 2^128 or 2^192 transitions
forkreturn the current stream and jump the parent
Rng4.withSeed, Rng4.initcreate four streams separated by canonical 2^128 jumps
Rng4.nextU64x4four independent full-width outputs as [u64; 4]
Rng4.nextFloatx4four independent values in [0, 1)
Rng4.fillBytesdeterministic lane-interleaved 32-byte batches

Bounded integers use Lemire multiply-high sampling with a rejection threshold, so arbitrary bounds are unbiased without division on the ordinary accepted path. Signed range width is calculated in unsigned space and therefore handles almost the complete i64 domain without overflow.

Reusable distributions ​

vex
import { Normal, Rng, WeightedIndex } from "rand"

let! rng = Rng.withSeed(42)
let! normal = match Normal.new(10.0, 2.0) {
    Result.Ok(value) => value,
    Result.Err(err) => $panic(err.message()),
}
let value = normal.sample(&rng!)
TypeConstructor contractSample behavior
Bernoullifinite probability in [0, 1]O(1); exact edges consume no RNG state
Normalfinite mean, finite non-negative deviationcached Box–Muller pair, zero allocation
Exponentialpositive finite ratestable log1p transform, zero allocation
WeightedIndexfinite non-negative weights, positive finite totalowned cumulative table, O(log n), zero allocation

All constructors return Result<..., DistributionError> and validate before a reusable distribution becomes observable.

Parallel streams ​

Give every worker its own Rng. Use longJump() to partition top-level jobs and jump() or fork() to partition worker streams. These operations neither allocate nor synchronize.

When one hot loop consumes four independent values together, Rng4 keeps the stream contract explicit and lets the backend legalize one fixed-array dataflow:

vex
import { Rng4 } from "rand"

let! streams = Rng4.withSeed(42)
let words = streams.nextU64x4()
let ratios = streams.nextFloatx4()

Lane zero is the ordinary seeded stream. Lanes one, two and three begin one, two and three jump() intervals later. Construction pays the partitioning cost once; steady-state calls have no allocation, lock, global state, FFI, or architecture-specific source. A partial Rng4.fillBytes step consumes one output from every lane, making state advancement deterministic.

Measured baseline ​

Apple M2 Max, release -O3, 2026-08-25, median of five 250 ms rounds:

OperationMedianAllocation
nextU643.13 ns0
Rng4.nextU64x44.55 ns / 1.14 ns per output0
Rng4.nextFloatx44.84 ns / 1.21 ns per output0
dynamic nextBelow3.25 ns0
1 KiB fillBytes170.22 ns / 5.60 GB/s0
1 KiB Rng4.fillBytes118.49 ns / 8.05 GB/s0
Bernoulli3.21 ns0
cached-pair Normal10.37 ns0
Exponential9.29 ns0
eight-entry WeightedIndex5.40 ns0

These values are target-specific evidence, not portable latency guarantees. On this Apple target, Rng4 produced about 2.75 times the scalar integer output throughput. NEON lacks packed u64 multiply, so the backend retains vector XOR/shift/rotate/load/store work and distributes the star-star multiply across scalar ALUs. Wider targets may legalize the same source differently.

Verification ​

bash
vex lint lib/std/rand --deny-warnings
vex test lib/std/rand/tests/basic.test.vx
vex test -O3 lib/std/rand/tests/basic.test.vx
vex test -O3 --bench --benchmem lib/std/rand/tests/bench.test.vx

Released under the MIT License.