jax.random.choice#

jax.random.choice(key, a, shape=(), replace=True, p=None, axis=0, mode=None)[source]#

Generates a random sample from a given array.

Warning

If p has fewer non-zero elements than the requested number of samples, as specified in shape, and replace=False, the output of this function is ill-defined. Please make sure to use appropriate inputs.

Parameters:
  • key (ArrayLike) – a PRNG key used as the random key.

  • a (int | ArrayLike) – array or int. If an ndarray, a random sample is generated from its elements. If an int, the random sample is generated as if a were arange(a).

  • shape (Shape) – tuple of ints, optional. Output shape. If the given shape is, e.g., (m, n), then m * n samples are drawn. Default is (), in which case a single value is returned.

  • replace (bool) – boolean. Whether the sample is with or without replacement. Default is True.

  • p (RealArray | None) – 1-D array-like, The probabilities associated with each entry in a. If not given the sample assumes a uniform distribution over all entries in a.

  • axis (int) – int, optional. The axis along which the selection is performed. The default, 0, selects by row.

  • mode (str | None) – optional, “high” or “low” for how many bits to use in the gumbel sampler when p is None and replace = False. The default is determined by the use_high_dynamic_range_gumbel config, which defaults to “low”. With mode=”low”, in float32 sampling will be biased for choices with probability less than about 1E-7; with mode=”high” this limit is pushed down to about 1E-14. mode=”high” approximately doubles the cost of sampling.

Returns:

An array of shape shape containing samples from a.

Return type:

Array