librarylibs/Random/src/Random.xtl
Random: effectful draws and distributions, plus pure Philox counter sampling and uniform-disk arrays. Import it with an alias of your choice: "r:" u_se< "Random". Put libs/Random/src on XETAL_PATH ("just path"); the reference is libs/Random/docs. Names with l: are exported; those under h: are private to this file.
Legacy draws use r_oll!, so "xetal run --seed N" makes them repeatable. Their ! suffix denotes effect order. Pure counter sampling below uses explicit seed/identity arrays and is independent of those effects.
Shuffling
ʰk̲eys! : Int -> Int
k random keys, 1..big.
ʰk̲eys! ← { k → r̲oll! k r̲eshape big }
ˡs̲huffle! : a -> a
s_huffle! v: the items of v in a random order (sorting random keys).
ˡs̲huffle! ← { v → (g̲rade ʰk̲eys! t̲ally v) s̲elect v }
ˡd̲eal! : Int -> Int -> Int
k d_eal! n: k different numbers from 1..n, in random order (APL's deal).
ˡd̲eal! ← { k n → (k < 0) ∨ k > n ? @ p̲anic< "cannot deal {k} different numbers from 1 to {n}" k t̲ake ˡs̲huffle! r̲ange n }
ˡc̲hoice! : a -> a
c_hoice! v: one item of v, each as likely.
ˡc̲hoice! ← { v → 0 = t̲ally v ? @ p̲anic< "there is nothing to choose from: the list is empty" (r̲oll! t̲ally v) s̲elect v }
ˡs̲ample! : Int -> a -> a
k s_ample! v: k items of v, each drawn afresh (with replacement).
ˡs̲ample! ← { k v → (0 = t̲ally v) ∧ k > 0 ? @ p̲anic< "there is nothing to sample from: the list is empty" (r̲oll! k r̲eshape t̲ally v) s̲elect v }
Sampling distributions
ˡu̲niform! : Int -> Float
u_niform! n: n Floats, each as likely anywhere in [0, 1).
ˡu̲niform! ← { n → (f̲loat (ʰk̲eys! n) − 1) ÷ f̲loat big }
ˡn̲ormal! : Int -> Float
n_ormal! n: n Floats from the standard normal distribution (mean 0, standard deviation 1), by the Box-Muller transform.
ˡn̲ormal! ← { n → a ← 1.0 − ˡu̲niform! n ⍝ in (0, 1], so l_og is finite b ← ˡu̲niform! n ((-2.0 × l̲og a) ^ 0.5) × c̲os 2.0 × b × p̲i @ }
ˡw̲eighted! : Num a => a -> Int -> Int
w w_eighted! k: k positions of w, each drawn with probability proportional to its weight (weights >= 0, not all 0).
ˡw̲eighted! ← { w k → c ← '+ s̲\ f̲loat w u ← (f̲irst -1 t̲ake c) × ˡu̲niform! k 1 + '{ t̲ally w̲here c ≤ ⍵ } e̲ach u }
ʰu̲int32 : Int -> Int
Unsigned 32-bit Int values, represented exactly in the language's Int64.
ʰu̲int32 ← { data → n̲ot '∧ r̲/ r̲avel (data ≥ 0) ∧ data ≤ 4294967295 ? @ p̲anic< "Random keyed coordinates must be unsigned 32-bit integers" data + 0 × data m̲od 1 }
ʰm̲ul32 : Int -> Int -> (Int, Int)
Exact high/low 32-bit product via 16-bit limbs; no Int64 overflow.
ʰm̲ul32 ← { multiplier values → al ← values m̲od 65536 ah ← values d̲iv 65536 bl ← multiplier m̲od 65536 bh ← multiplier d̲iv 65536 low ← al × bl middle ← (ah × bl) + (al × bh) + low d̲iv 65536 hi ← (ah × bh) + middle d̲iv 65536 lo ← (low m̲od 65536) + 65536 × middle m̲od 65536 (hi, lo) }
ʰc̲olumns : a -> a -> a -> a -> a
Four Int column vectors to an N by 4 matrix, including an empty batch.
ʰc̲olumns ← { a b c d → n ← t̲ally a o̲\ ((1 c̲at n) r̲eshape a) c̲at ((1 c̲at n) r̲eshape b) c̲at ((1 c̲at n) r̲eshape c) c̲at (1 c̲at n) r̲eshape d }
ˡp̲hilox : Int -> Int -> Int
key p_hilox counters: Philox4x32-10, key length 2 Int, counters N by 4 Int, all uint32. Returns N by 4 exact unsigned words in Int64 arrays. Original arithmetic implementation of the published Philox equations.
ˡp̲hilox ← { key counters → n̲ot (1 r̲eshape 2) m̲atch s̲hape key ? @ p̲anic< "Random Philox key requires two words" n̲ot ((t̲ally counters) c̲at 4) m̲atch s̲hape counters ? @ p̲anic< "Random Philox counters require N by 4 words" k ← ʰu̲int32 key ctr ← ʰu̲int32 counters round̲ ← { state → (c, keys) ← state (hi0, lo0) ← 3528531795 ʰm̲ul32 1 s̲elect₂ c (hi1, lo1) ← 3449720151 ʰm̲ul32 3 s̲elect₂ c a ← hi1 ᵇx̲or (2 s̲elect₂ c) ᵇx̲or f̲irst keys cc ← hi0 ᵇx̲or (4 s̲elect₂ c) ᵇx̲or f̲irst 1 d̲rop keys next ← (((a ʰc̲olumns lo1) cc) lo0) (next, (keys + 2654435769 3144134277) m̲od 4294967296) } (result, _) ← 10 'round̲ p̲ower (ctr, k) result }
ˡs̲ampleKeys : Int -> Int -> Int
seed s_ampleKeys identities: scalar uint32 seed and N by 4 uint32 [pixel,sample,interaction,dimension] to N exact Int words. Seed maps to Philox key [seed,0]; the result is counter output word 1.
ˡs̲ampleKeys ← { seed identities → n̲ot 0 = t̲ally s̲hape seed ? @ p̲anic< "Random sampling seed must be scalar" checked ← ʰu̲int32 seed f̲irst₂ (checked c̲at 0) ˡp̲hilox identities }
ˡs̲amples : Int -> Int -> Float
seed s_amples identities: same counter identity, Float midpoints from the high 23 bits: (floor(word/512)+0.5)/8388608. Exactly representable in Float32 and Float64, strictly in (0,1), no dependence on effect order.
ˡs̲amples ← { seed identities → words ← seed ˡs̲ampleKeys identities ((f̲loat words d̲iv 512) + 0.5) ÷ 8388608.0 }
ˡd̲isk : Num a => a -> Float
d_isk square: N by 2 finite unit-square samples to N by 2 uniform-disk coordinates using radius sqrt(u), azimuth 2*pi*v; endpoints are valid.
ˡd̲isk ← { square → n̲ot ((t̲ally square) c̲at 2) m̲atch s̲hape square ? @ p̲anic< "Random disk sampling requires N by 2 square samples" a ← f̲loat square n̲ot '∧ r̲/ r̲avel (a ≥ 0.0) ∧ (a ≤ 1.0) ∧ (a − a) = 0.0 ? @ p̲anic< "Random disk square samples must be finite in 0..1" radius ← s̲qrt 1 s̲elect₂ a angle ← (2.0 × p̲i @) × 2 s̲elect₂ a x ← radius × c̲os angle y ← radius × s̲in angle n ← t̲ally a o̲\ ((1 c̲at n) r̲eshape x) c̲at (1 c̲at n) r̲eshape y }