Skip to main content

Module random

Module random 

Source
Expand description

Random numbers following jax.random, bit for bit.

Keys hold two u32 words, as JAX’s default threefry2x32 keys. The hash is written generically with the same structure as JAX’s lowering (a loop of five iterations of four rounds, and counters built from a u64 iota), so traced programs compile to the same HLO as JAX and native programs compute the same numbers.

§Examples

use ruxla_core::{Native, random};

let key = random::key::<Native>(0);
let (k1, k2) = random::split2(&key);
let x = random::uniform::<Native, f32>(&k1, [3], 0.0, 1.0);
assert!(x.to_vec().iter().all(|&v| (0.0..1.0).contains(&v)));
let n = random::randint::<Native, i32>(&k2, [], 0, 10).to_scalar();
assert!((0..10).contains(&n.value()));

Structs§

Key
A random key: two u32 words (or an array of keys, shape [..., 2]).

Functions§

batched_uniform
Uniform random floats in [minval, maxval) for each key of an array of keys (u32[*batch, 2]), of shape [*batch, *shape]: JAX’s jax.vmap(lambda k: jax.random.uniform(k, shape, minval, maxval))(keys) (one vmap per batch dimension), with the keys hashed together as JAX’s batched threefry2x32 hashes them. minval and maxval broadcast against the result, as unbatched operands of the vmapped function.
bernoulli
Random booleans that are true with probability p (jax.random.bernoulli).
beta
Beta random floats (jax.random.beta).
binomial
Binomial random floats: the number of successes in n trials of probability p (jax.random.binomial).
bits
Uniform random u32 bits (jax.random.bits).
categorical
Samples indices (i32) from the categorical distribution of logits along axis (jax.random.categorical).
cauchy
Cauchy random floats (jax.random.cauchy).
chisquare
Chi-square random floats with df degrees of freedom (jax.random.chisquare).
choice
Random elements of a along axis 0 (jax.random.choice with p=None and axis=0), with or without replacement. Use jnp::arange for the integer form choice(key, n, ...).
choice_with_p
Random elements of a along axis 0 with probabilities p (jax.random.choice with p and axis=0), with or without replacement.
dirichlet
Dirichlet random floats (jax.random.dirichlet): alpha has shape [..., k], and the result has shape batch_shape + [k].
double_sided_maxwell
Double-sided Maxwell random floats with location loc and scale scale (jax.random.double_sided_maxwell).
exponential
Exponential random floats with rate 1 (jax.random.exponential).
f
F random floats (jax.random.f).
fold_in
Folds data into a key (jax.random.fold_in).
gamma
Gamma random floats with shape a and rate 1 (jax.random.gamma), differentiable with respect to a.
gumbel
Standard Gumbel random floats (jax.random.gumbel).
key
A key from a seed, like jax.random.key.
laplace
Laplace random floats (jax.random.laplace).
loggamma
The logarithms of gamma random floats (jax.random.loggamma), accurate for small a.
logistic
Logistic random floats (jax.random.logistic).
maxwell
Maxwell random floats (jax.random.maxwell): the norms of standard normal vectors of three elements.
normal
Standard normal random floats (jax.random.normal).
pareto
Pareto random floats with shape b (jax.random.pareto).
permutation
A random permutation of 0..n (jax.random.permutation(key, n)).
poisson
Poisson random integers with rate lam (jax.random.poisson).
rademacher
Random signs: -1 or 1 with equal probability (jax.random.rademacher).
randint
Uniform random integers in [minval, maxval) (jax.random.randint).
shuffle
x with its elements along axis 0 randomly permuted (jax.random.permutation(key, x), with axis=0 and independent=False): the rows are taken in the order of permutation of their number.
split
Splits a key into num keys (jax.random.split).
split2
Splits a key in two.
t
Student’s t random floats with df degrees of freedom (jax.random.t).
truncated_normal
Standard normal random floats truncated to (lower, upper) (jax.random.truncated_normal).
uniform
Uniform random floats in [minval, maxval) (jax.random.uniform).
weibull_min
Weibull random floats (jax.random.weibull_min).