March Docs

Random

Random module: purely-functional pseudorandom number generation.

Algorithm: xoshiro256 adapted for 63-bit integers (March/OCaml native int). State: 4 × 63-bit integers packed into the Rng record. Seeding: SplitMix-inspired hash expansion from a single integer.

Usage: let rng = Random.seed(42) let (x, rng2) = Random.next_int(rng) let (f, rng3) = Random.next_float(rng2)

All functions are pure: they take an Rng and return (value, new_Rng). No global mutable state.

Types

typeRngRng = { s0 : Int, s1 : Int, s2 : Int, s3 : Int }#

Functions

fnseedseed(n : Int) : Rng#

Create a deterministic Rng from an integer seed.

fnnext_intnext_int(rng : Rng) : (Int, Rng)#

Generate the next pseudorandom integer (full 63-bit signed range).

fnnext_floatnext_float(rng : Rng) : (Float, Rng)#

Generate a pseudorandom float in [0.0, 1.0).

fnrangerange(rng : Rng, lo : Int, hi : Int) : (Int, Rng)#

Generate a random integer in [lo, hi] (inclusive).

fnintint(lo : Int, hi : Int) : Int#

Generate a random integer in [lo, hi] (inclusive) using the current time as seed.

fnshuffleshuffle(rng : Rng, xs : List(a)) : (List(a), Rng)#

Shuffle a list using the Fisher-Yates algorithm. Returns (shuffled_list, new_rng). O(n²) on lists. Same seed + same list always produces the same shuffle.

fnsamplesample(rng : Rng, xs : List(a), n : Int) : (List(a), Rng)#

Sample n elements from xs without replacement. If n >= length(xs), returns all elements (shuffled). Returns (sample, new_rng).