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
u32words (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’sjax.vmap(lambda k: jax.random.uniform(k, shape, minval, maxval))(keys)(one vmap per batch dimension), with the keys hashed together as JAX’s batchedthreefry2x32hashes them.minvalandmaxvalbroadcast 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
ntrials of probabilityp(jax.random.binomial). - bits
- Uniform random
u32bits (jax.random.bits). - categorical
- Samples indices (
i32) from the categorical distribution oflogitsalongaxis(jax.random.categorical). - cauchy
- Cauchy random floats (
jax.random.cauchy). - chisquare
- Chi-square random floats with
dfdegrees of freedom (jax.random.chisquare). - choice
- Random elements of
aalong axis 0 (jax.random.choicewithp=Noneandaxis=0), with or without replacement. Usejnp::arangefor the integer formchoice(key, n, ...). - choice_
with_ p - Random elements of
aalong axis 0 with probabilitiesp(jax.random.choicewithpandaxis=0), with or without replacement. - dirichlet
- Dirichlet random floats (
jax.random.dirichlet):alphahas shape[..., k], and the result has shapebatch_shape + [k]. - double_
sided_ maxwell - Double-sided Maxwell random floats with location
locand scalescale(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
aand rate 1 (jax.random.gamma), differentiable with respect toa. - 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 smalla. - 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
xwith its elements along axis 0 randomly permuted (jax.random.permutation(key, x), withaxis=0andindependent=False): the rows are taken in the order ofpermutationof their number.- split
- Splits a key into
numkeys (jax.random.split). - split2
- Splits a key in two.
- t
- Student’s t random floats with
dfdegrees 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).