sourcelibs/Random/src/Random.xtl

1⍝│ Random: effectful draws and distributions, plus pure Philox counter 2⍝│ sampling and uniform-disk arrays. 3⍝│ Import it with an alias of your choice: "r:" u_se< "Random". 4⍝│ Put libs/Random/src on XETAL_PATH ("just path"); the reference is libs/Random/docs. 5⍝│ Names with l: are exported; those under h: are private to this file. 6⍝│ 7⍝│ Legacy draws use r_oll!, so "xetal run --seed N" makes them repeatable. 8⍝│ Their ! suffix denotes effect order. Pure counter sampling below uses 9⍝│ explicit seed/identity arrays and is independent of those effects. 10 11big ← 1000000000 ⍝ the resolution of a draw 12 13⍝━━ Shuffling 14 15⍝│ k random keys, 1..big. 16ʰk̲eys! ← { k → r̲oll! k r̲eshape big } 17 18⍝│ s_huffle! v: the items of v in a random order (sorting random keys). 19ˡs̲huffle! ← { v → (g̲rade ʰk̲eys! t̲ally v) s̲elect v } 20 21⍝│ k d_eal! n: k different numbers from 1..n, in random order (APL's deal). 22ˡd̲eal! ← { k n → 23 (k < 0) ∨ k > n ? @ p̲anic< "cannot deal {k} different numbers from 1 to {n}"
p̲anic< expands to
(⎕P̲ANIC ("cannot deal " c̲at (f̲ormat (k)) c̲at " different numbers from 1 to " c̲at (f̲ormat (n))))
24 k t̲ake ˡs̲huffle! r̲ange n 25} 26 27⍝│ c_hoice! v: one item of v, each as likely. 28ˡc̲hoice! ← { v → 29 0 = t̲ally v ? @ p̲anic< "there is nothing to choose from: the list is empty"
p̲anic< expands to
(⎕P̲ANIC ("there is nothing to choose from: the list is empty"))
30 (r̲oll! t̲ally v) s̲elect v 31} 32 33⍝│ k s_ample! v: k items of v, each drawn afresh (with replacement). 34ˡs̲ample! ← { k v → 35 (0 = t̲ally v) ∧ k > 0 ? @ p̲anic< "there is nothing to sample from: the list is empty"
p̲anic< expands to
(⎕P̲ANIC ("there is nothing to sample from: the list is empty"))
36 (r̲oll! k r̲eshape t̲ally v) s̲elect v 37} 38 39⍝━━ Sampling distributions 40 41⍝│ u_niform! n: n Floats, each as likely anywhere in [0, 1). 42ˡu̲niform! ← { n → (f̲loat (ʰk̲eys! n) − 1) ÷ f̲loat big } 43 44⍝│ n_ormal! n: n Floats from the standard normal distribution (mean 0, 45⍝│ standard deviation 1), by the Box-Muller transform. 46ˡn̲ormal! ← { n → 47 a ← 1.0 − ˡu̲niform! n ⍝ in (0, 1], so l_og is finite 48 b ← ˡu̲niform! n 49 ((-2.0 × l̲og a) ^ 0.5) × c̲os 2.0 × b × p̲i @ 50} 51 52⍝│ w w_eighted! k: k positions of w, each drawn with probability 53⍝│ proportional to its weight (weights >= 0, not all 0). 54ˡw̲eighted! ← { w k → 55 c ← '+ s̲\ f̲loat w 56 u ← (f̲irst -1 t̲ake c) × ˡu̲niform! k 57 1 + '{ t̲ally w̲here c ≤ ⍵ } e̲ach u 58} 59 60"b:" u̲se< "Bits" 61 62⍝│ Unsigned 32-bit Int values, represented exactly in the language's Int64. 63ʰu̲int32 ← { data → 64 n̲ot '∧ r̲/ r̲avel (data ≥ 0) ∧ data ≤ 4294967295 ? @ p̲anic< "Random keyed coordinates must be unsigned 32-bit integers"
p̲anic< expands to
(⎕P̲ANIC ("Random keyed coordinates must be unsigned 32-bit integers"))
65 data + 0 × data m̲od 1 66} 67 68⍝│ Exact high/low 32-bit product via 16-bit limbs; no Int64 overflow. 69ʰm̲ul32 ← { multiplier values → 70 al ← values m̲od 65536 71 ah ← values d̲iv 65536 72 bl ← multiplier m̲od 65536 73 bh ← multiplier d̲iv 65536 74 low ← al × bl 75 middle ← (ah × bl) + (al × bh) + low d̲iv 65536 76 hi ← (ah × bh) + middle d̲iv 65536 77 lo ← (low m̲od 65536) + 65536 × middle m̲od 65536 78 (hi, lo) 79} 80 81⍝│ Four Int column vectors to an N by 4 matrix, including an empty batch. 82ʰc̲olumns ← { a b c d → 83 n ← t̲ally a 84 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 85} 86 87⍝│ key p_hilox counters: Philox4x32-10, key length 2 Int, counters N by 4 88⍝│ Int, all uint32. Returns N by 4 exact unsigned words in Int64 arrays. 89⍝│ Original arithmetic implementation of the published Philox equations. 90ˡp̲hilox ← { key counters → 91 n̲ot (1 r̲eshape 2) m̲atch s̲hape key ? @ p̲anic< "Random Philox key requires two words"
p̲anic< expands to
(⎕P̲ANIC ("Random Philox key requires two words"))
92 n̲ot ((t̲ally counters) c̲at 4) m̲atch s̲hape counters ? @ p̲anic< "Random Philox counters require N by 4 words"
p̲anic< expands to
(⎕P̲ANIC ("Random Philox counters require N by 4 words"))
93 k ← ʰu̲int32 key 94 ctr ← ʰu̲int32 counters 95 round̲ ← { state → 96 (c, keys) ← state 97 (hi0, lo0) ← 3528531795 ʰm̲ul32 1 s̲elect₂ c 98 (hi1, lo1) ← 3449720151 ʰm̲ul32 3 s̲elect₂ c 99 a ← hi1 ᵇx̲or (2 s̲elect₂ c) ᵇx̲or f̲irst keys 100 cc ← hi0 ᵇx̲or (4 s̲elect₂ c) ᵇx̲or f̲irst 1 d̲rop keys 101 next ← (((a ʰc̲olumns lo1) cc) lo0) 102 (next, (keys + 2654435769 3144134277) m̲od 4294967296) 103 } 104 (result, _) ← 10 'round̲ p̲ower (ctr, k) 105 result 106} 107 108⍝│ seed s_ampleKeys identities: scalar uint32 seed and N by 4 uint32 109⍝│ [pixel,sample,interaction,dimension] to N exact Int words. Seed maps to 110⍝│ Philox key [seed,0]; the result is counter output word 1. 111ˡs̲ampleKeys ← { seed identities → 112 n̲ot 0 = t̲ally s̲hape seed ? @ p̲anic< "Random sampling seed must be scalar"
p̲anic< expands to
(⎕P̲ANIC ("Random sampling seed must be scalar"))
113 checked ← ʰu̲int32 seed 114 f̲irst₂ (checked c̲at 0) ˡp̲hilox identities 115} 116 117⍝│ seed s_amples identities: same counter identity, Float midpoints from 118⍝│ the high 23 bits: (floor(word/512)+0.5)/8388608. Exactly representable 119⍝│ in Float32 and Float64, strictly in (0,1), no dependence on effect order. 120ˡs̲amples ← { seed identities → 121 words ← seed ˡs̲ampleKeys identities 122 ((f̲loat words d̲iv 512) + 0.5) ÷ 8388608.0 123} 124 125⍝│ d_isk square: N by 2 finite unit-square samples to N by 2 uniform-disk 126⍝│ coordinates using radius sqrt(u), azimuth 2*pi*v; endpoints are valid. 127ˡd̲isk ← { square → 128 n̲ot ((t̲ally square) c̲at 2) m̲atch s̲hape square ? @ p̲anic< "Random disk sampling requires N by 2 square samples"
p̲anic< expands to
(⎕P̲ANIC ("Random disk sampling requires N by 2 square samples"))
129 a ← f̲loat square 130 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"
p̲anic< expands to
(⎕P̲ANIC ("Random disk square samples must be finite in 0..1"))
131 radius ← s̲qrt 1 s̲elect₂ a 132 angle ← (2.0 × p̲i @) × 2 s̲elect₂ a 133 x ← radius × c̲os angle 134 y ← radius × s̲in angle 135 n ← t̲ally a 136 o̲\ ((1 c̲at n) r̲eshape x) c̲at (1 c̲at n) r̲eshape y 137}