~/bend-docscommunity

rng.bend checks

raw source on the hub · import bend-ndarray@0.1.2.0/rng.bend as Rng

bend-ndarray's random numbers, pure: the Philox4x32-10 counter-based generator (Salmon et al., "Parallel random numbers: as easy as 1, 2, 3", the generator of Random123, JAX and PyTorch) and the draws built on it, one array element at a time. Element i of a draw (row-major) is a function of the seed, the draw's number and i alone, so an array is generated in any order, in parallel, with the same result.

1 import
import Base

Types

type HL source · line 9 · raw

Data

type Q4 source · line 23 · raw

Data

type Gen source · line 56 · raw

Data

a seed (as two words) and how many draws it has made

type Dist source · line 127 · raw

Data

type KI source · line 160 · raw

Data

Definitions

def lo16 source · line 12 · raw

@+x:U32 -> U32

def hi16 source · line 15 · raw

@+x:U32 -> U32

def mulhilo_mid source · line 18 · raw

@+mid:U32 -> @+hh:U32 -> @+lh:U32 -> @+hl:U32 -> @+lo:U32 -> HL

def mulhilo_go source · line 26 · raw

@+al:U32 -> @+ah:U32 -> @+bl:U32 -> @+bh:U32 -> @+lo:U32 -> HL

def mulhilo source · line 29 · raw

@+a:U32 -> @+b:U32 -> HL

def round_mix source · line 32 · raw

@p0:HL -> @p1:HL -> @+c1:U32 -> @+c3:U32 -> @+k0:U32 -> @+k1:U32 -> Q4

def round source · line 37 · raw

@q:Q4 -> @+k0:U32 -> @+k1:U32 -> Q4

def rounds source · line 42 · raw

@n:Nat -> @q:Q4 -> @+k0:U32 -> @+k1:U32 -> Q4

def philox source · line 50 · raw

@q:Q4 -> @+k0:U32 -> @+k1:U32 -> Q4

the four words of counter (c0, c1, c2, c3) under key (k0, k1)

def rng source · line 60 · raw

@+seed:U32 -> Gen

the generator for a 32-bit seed

def rng64 source · line 64 · raw

@+hi:U32 -> @+lo:U32 -> Gen

the generator for a 64-bit seed, as its high and low words

def next source · line 67 · raw

@g:Gen -> Gen

def words source · line 73 · raw

@+k0:U32 -> @+k1:U32 -> @+n:U32 -> @+i:U32 -> @+j:U32 -> Q4

the words of element i of draw n, try j (an integer draw that rejects a word tries again)

def qa source · line 76 · raw

@q:Q4 -> U32

def qb source · line 81 · raw

@q:Q4 -> U32

def unit source · line 87 · raw

@+x:U32 -> F32

a word to a float32 in [0, 1): its top 24 bits times 2^-24 (NumPy's float32 method)

def unit_pos source · line 91 · raw

@+x:U32 -> F32

in (0, 1]: for the logarithm of Box-Muller

def bits source · line 94 · raw

@x:F32 -> U32

def reject_below source · line 103 · raw

@+r:U32 -> U32

2^32 mod r: products whose low word is below it are rejected

def lemire_pick source · line 106 · raw

@p:HL -> @+r:U32 -> @more:(@_:Unit -> U32) -> U32

def lemire_lanes source · line 111 · raw

@q:Q4 -> @+r:U32 -> @more:(@_:Unit -> U32) -> U32

def lemire source · line 118 · raw

@fuel:Nat -> @+k0:U32 -> @+k1:U32 -> @+n:U32 -> @+i:U32 -> @+j:U32 -> @+r:U32 -> U32

an offset in [0, r) for element i, trying up to fuel blocks of four words (0 if all were rejected: probability below 2^-(4 fuel))

def normal01 source · line 134 · raw

@q:Q4 -> F32

def cell source · line 140 · raw

@d:Dist -> @+k0:U32 -> @+k1:U32 -> @+n:U32 -> @+i:U32 -> U32

element i's cell of draw n

def key_of source · line 153 · raw

@q:Q4 -> HL

def key2 source · line 163 · raw

@+k0:U32 -> @+k1:U32 -> @+n:U32 -> @+i:U32 -> HL

def key_le source · line 166 · raw

@x:HL -> @y:HL -> Bool

def ki_le source · line 171 · raw

@x:KI -> @y:KI -> Bool

def merge_pick source · line 176 · raw

@le:Bool -> @+x:KI -> @+xt:List<&2, KI> -> @+y:KI -> @+yt:List<&2, KI> -> @rest_x:(@_:Unit -> List<&2, KI>) -> @rest_y:(@_:Unit -> List<&2, KI>) -> List<&2, KI>

def merge source · line 183 · raw

@xs:List<&2, KI> -> @ys:List<&2, KI> -> List<&2, KI>

def evens source · line 192 · raw

@xs:List<&2, KI> -> List<&2, KI>

def odds source · line 201 · raw

@xs:List<&2, KI> -> List<&2, KI>

def msort source · line 210 · raw

@fuel:Nat -> @+xs:List<&2, KI> -> List<&2, KI>

def idxs source · line 222 · raw

@xs:List<&2, KI> -> List<&2, Nat>

def keyed source · line 230 · raw

@c:Nat -> @+k0:U32 -> @+k1:U32 -> @+n:U32 -> @+start:Nat -> List<&2, KI>

indices start .. start + c - 1, each with its key (the 64-bit key of draw n's element of that index)

def shuffled source · line 238 · raw

@+k0:U32 -> @+k1:U32 -> @+n:U32 -> @+m:Nat -> List<&2, Nat>

0 .. m - 1 in a random order: each index with the 64-bit key of draw n's element of that index, sorted by key