~/bend-docscommunity

rng.bend checks

raw source on the hub · import bend-ndarray@0.1.4.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.

4 imports
import Base
import bend-float64@0.1.8.0/wide.bend as W
import bend-float64@0.1.8.0/float64.bend as F
import bend-float64@0.1.8.0/math.bend as M

Types

type HL source · line 12 · raw

Data

type Q4 source · line 26 · raw

Data

type Gen source · line 59 · raw

Data

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

type Dist source · line 130 · raw

Data

type Dist64 source · line 175 · raw

Data

type KI source · line 197 · raw

Data

Definitions

def lo16 source · line 15 · raw

@+x:U32 -> U32

def hi16 source · line 18 · raw

@+x:U32 -> U32

def mulhilo_mid source · line 21 · raw

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

def mulhilo_go source · line 29 · raw

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

def mulhilo source · line 32 · raw

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

def round_mix source · line 35 · raw

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

def round source · line 40 · raw

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

def rounds source · line 45 · raw

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

def philox source · line 53 · 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 63 · raw

@+seed:U32 -> Gen

the generator for a 32-bit seed

def rng64 source · line 67 · raw

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

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

def next source · line 70 · raw

@g:Gen -> Gen

def words source · line 76 · 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 79 · raw

@q:Q4 -> U32

def qb source · line 84 · raw

@q:Q4 -> U32

def unit source · line 90 · 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 94 · raw

@+x:U32 -> F32

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

def bits source · line 97 · raw

@x:F32 -> U32

def reject_below source · line 106 · raw

@+r:U32 -> U32

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

def lemire_pick source · line 109 · raw

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

def lemire_lanes source · line 114 · raw

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

def lemire source · line 121 · 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 137 · raw

@q:Q4 -> F32

def cell source · line 143 · raw

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

element i's cell of draw n

def unit64_go source · line 160 · raw

@+a:U32 -> @+b:U32 -> @+plus:U32 -> 0x72aa159312099184b98e33378b057a0a/float64.F64

two words to a float64 in [0, 1): a's top 27 bits and b's top 26 as a 53-bit integer, times 2^-53 (NumPy's float64 method); plus one 2^-53 for (0, 1]

def unit64 source · line 164 · raw

@q:Q4 -> 0x72aa159312099184b98e33378b057a0a/float64.F64

def normal64 source · line 170 · raw

@q:Q4 -> 0x72aa159312099184b98e33378b057a0a/float64.F64

a normal deviate from four words (Box-Muller: sqrt(-2 log u) cos(2 pi v), u in (0, 1] from a and b, v from c and d)

def cell64 source · line 181 · raw

@d:Dist64 -> @+k0:U32 -> @+k1:U32 -> @+n:U32 -> @+i:U32 -> 0x72aa159312099184b98e33378b057a0a/float64.F64

element i's float64 of draw n

def key_of source · line 190 · raw

@q:Q4 -> HL

def key2 source · line 200 · raw

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

def key_le source · line 203 · raw

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

def ki_le source · line 208 · raw

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

def merge_pick source · line 213 · 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 220 · raw

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

def evens source · line 229 · raw

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

def odds source · line 238 · raw

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

def msort source · line 247 · raw

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

def idxs source · line 259 · raw

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

def keyed source · line 267 · 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 275 · 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