Expand description
Random numbers from explicit keys or a per-thread key sequence, bit for bit as in MLX.
A key is a uint32 array of shape [2]. bits hashes counters with threefry2x32
using MLX’s layout, and uniform and normal are composed from operations
exactly as in MLX’s random.cpp. For the same seed, bits and uniform samples equal MLX’s.
Normal samples agree within a few ulps: erfinv needs a logarithm, whose last bits
differ between implementations.
Without a key, functions draw the next key from this thread’s sequence, which starts from
a per-process time seed until seed is called (as in MLX).
Functions§
- bernoulli
- Booleans of
shapethat are true with probabilityp, as MLX:uniform < p. - bits
- Random bits of
shape,widthbytes per element (1, 2 or 4 → uint8, uint16, uint32). - categorical
- Indices (uint32) drawn from the categorical distributions whose unnormalised log
probabilities run along
axisoflogits, as MLX: the argmax oflogitsplus Gumbel noise. The result haslogits’ shape withoutaxis. - gumbel
- Gumbel samples of
shapeand floating-pointdtype, as MLX:−log(−log(u))foruuniform in[0, 1). - key
- A key for
seed:[seed >> 32, seed & 0xffffffff]. - next_
key - The next key of this thread’s sequence (
splitthe current key; keep the first half), the key that functions called without one use. Useful to pass the sequence’s state into a compiled function, where keys drawn inside would be frozen at the first trace. - normal
- Normal samples (mean
loc, standard deviationscale) ofshapeand floating-pointdtype, as MLX:√2 · erfinv(u)foruuniform in(-1, 1). - randint
- Integers in
[low, high)ofshapeand integerdtype, as MLX: uniform floats in[low, high)rounded down. - seed
- Restarts this thread’s key sequence from
seed. - split
numnew keys fromkey, as an array of shape[num, 2].- split_
pair - Two new keys from
key. - uniform
- Uniform samples in
[low, high)ofshapeand floating-pointdtype(float16, bfloat16 or float32), as MLX: 32 random bits divided byu32::MAX, clamped below 1.