veko doc
Self-Contained API Reference • Gradual Types • Capabilities • Wasm Sandbox

rand

lib/rand.vk

lib/rand.vk Pure-Veko deterministic PRNG (SplitMix64 + xoshiro256++), Fisher-Yates shuffle, CSPRNG wrappers, and RFC 4122 UUID v4 generation. Zero capabilities required.

Rng struct
signature
Rng = struct { s0 i64, s1 i64, s2 i64, s3 i64 }

Deterministic xoshiro256++ pseudo-random number generator (256-bit state). Not for cryptographic keys; use rand/csprng-bytes or random/bytes for cryptography.

signature
splitmix64-step = fn [seed i64] map

One SplitMix64 mixing step returning { val = i64, next_seed = i64 }.

seed fn
signature
seed = fn [seed_val i64] Rng

Seeds a deterministic xoshiro256++ Rng from a 64-bit seed via SplitMix64.

Executable Example
veko
use [rand]
mut r1 = rand/seed 42
mut r2 = rand/seed 42
a = rand/next-u64 mut r1
b = rand/next-u64 mut r2
if a != b or a == 0 [ err 'rand/seed' ] else [ .ok ]
signature
next-u64 = fn [mut r Rng] i64

Advances r using xoshiro256++ and returns the next 64-bit word.

signature
range = fn [mut r Rng, lo i64, hi i64] i64

Returns a pseudo-random integer uniformly in [lo, hi) (lo < hi).

Executable Example
veko
use [rand]
mut r = rand/seed 7
v = rand/range mut r 10 20
if v < 10 or v >= 20 [ err 'rand/range' ] else [ .ok ]
signature
f64-unit = fn [mut r Rng] f64

Returns a uniform f64 in [0.0, 1.0) using 53 bits of precision.

signature
bytes-fill = fn [mut r Rng, n i64] bytes

Produces n pseudo-random bytes from r.

signature
shuffle = fn [mut r Rng, mut xs block] i64

In-place Fisher-Yates shuffle of xs using r.

Executable Example
veko
use [rand]
mut r = rand/seed 99
mut xs = [1 2 3 4 5]
_ = rand/shuffle mut r mut xs
if (len xs) != 5 [ err 'shuffle' ] else [ .ok ]
signature
choose = fn [mut r Rng, xs block] any

Picks a random element from non-empty block xs.

signature
csprng-bytes = fn [n i64] bytes

Returns n cryptographically secure random bytes from the OS/hardware CSPRNG (random/bytes).

signature
uuid-v4 = fn [] str

Generates a random RFC 4122 Version 4 UUID string (xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx) using the OS/hardware CSPRNG.

Executable Example
veko
use [rand]
u = rand/uuid-v4
if (len u) != 36 or u[14..15] != '4' [ err 'uuid-v4' ] else [ .ok ]
signature
uuid-v7-at = fn [unix_ms i64] str

Generates an RFC 9562 time-ordered Version 7 UUID string (xxxxxxxx-xxxx-7xxx-yxxx-xxxxxxxxxxxx) from a caller-provided 48-bit Unix millisecond timestamp unix_ms and OS CSPRNG entropy.

Executable Example
veko
use [rand]
u1 = rand/uuid-v7-at 1700000000000
u2 = rand/uuid-v7-at 1700000001000
if (len u1) != 36 or u1[14..15] != '7' or u1[0..13] == u2[0..13] [ err 'uuid-v7-at' ] else [ .ok ]
uuid-v7 fn caps: .clocks
signature
uuid-v7 = fn [] str

Generates an RFC 9562 Version 7 UUID using the live UTC wall clock (time/unix) and OS CSPRNG.

Executable Example
veko
use [rand]
u = rand/uuid-v7
if (len u) != 36 or u[14..15] != '7' [ err 'uuid-v7' ] else [ .ok ]