rand
lib/rand.vklib/rand.vk Pure-Veko deterministic PRNG (SplitMix64 + xoshiro256++), Fisher-Yates shuffle, CSPRNG wrappers, and RFC 4122 UUID v4 generation. Zero capabilities required.
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.
splitmix64-step = fn [seed i64] map
One SplitMix64 mixing step returning { val = i64, next_seed = i64 }.
seed = fn [seed_val i64] Rng
Seeds a deterministic xoshiro256++ Rng from a 64-bit seed via SplitMix64.
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 ]
next-u64 = fn [mut r Rng] i64
Advances r using xoshiro256++ and returns the next 64-bit word.
range = fn [mut r Rng, lo i64, hi i64] i64
Returns a pseudo-random integer uniformly in [lo, hi) (lo < hi).
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 ]
f64-unit = fn [mut r Rng] f64
Returns a uniform f64 in [0.0, 1.0) using 53 bits of precision.
shuffle = fn [mut r Rng, mut xs block] i64
In-place Fisher-Yates shuffle of xs using r.
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 ]
csprng-bytes = fn [n i64] bytes
Returns n cryptographically secure random bytes from the OS/hardware CSPRNG (random/bytes).
uuid-v4 = fn [] str
Generates a random RFC 4122 Version 4 UUID string (xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx)
using the OS/hardware CSPRNG.
use [rand]
u = rand/uuid-v4
if (len u) != 36 or u[14..15] != '4' [ err 'uuid-v4' ] else [ .ok ]
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.
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 [] str
Generates an RFC 9562 Version 7 UUID using the live UTC wall clock (time/unix) and OS CSPRNG.
use [rand]
u = rand/uuid-v7
if (len u) != 36 or u[14..15] != '7' [ err 'uuid-v7' ] else [ .ok ]