2023-11-30 04:41:56 +08:00
|
|
|
.. _random:
|
|
|
|
|
|
|
|
Random
|
|
|
|
======
|
|
|
|
|
|
|
|
Random sampling functions in MLX use an implicit global PRNG state by default.
|
|
|
|
However, all function take an optional ``key`` keyword argument for when more
|
|
|
|
fine-grained control or explicit state management is needed.
|
|
|
|
|
|
|
|
For example, you can generate random numbers with:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
for _ in range(3):
|
|
|
|
print(mx.random.uniform())
|
|
|
|
|
|
|
|
which will print a sequence of unique pseudo random numbers. Alternatively you
|
|
|
|
can explicitly set the key:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
key = mx.random.key(0)
|
|
|
|
for _ in range(3):
|
|
|
|
print(mx.random.uniform(key=key))
|
|
|
|
|
|
|
|
which will yield the same pseudo random number at each iteration.
|
|
|
|
|
|
|
|
Following `JAX's PRNG design <https://jax.readthedocs.io/en/latest/jep/263-prng.html>`_
|
|
|
|
we use a splittable version of Threefry, which is a counter-based PRNG.
|
|
|
|
|
|
|
|
.. currentmodule:: mlx.core.random
|
|
|
|
|
|
|
|
.. autosummary::
|
|
|
|
:toctree: _autosummary
|
|
|
|
|
|
|
|
bernoulli
|
|
|
|
categorical
|
|
|
|
gumbel
|
2024-01-18 09:15:29 +08:00
|
|
|
key
|
2023-11-30 04:41:56 +08:00
|
|
|
normal
|
2024-04-12 08:33:33 +08:00
|
|
|
multivariate_normal
|
2023-11-30 04:41:56 +08:00
|
|
|
randint
|
2024-01-18 09:15:29 +08:00
|
|
|
seed
|
|
|
|
split
|
2023-11-30 04:41:56 +08:00
|
|
|
truncated_normal
|
2024-01-18 09:15:29 +08:00
|
|
|
uniform
|
2024-07-26 02:59:11 +08:00
|
|
|
laplace
|
2024-10-15 04:10:48 +08:00
|
|
|
permutation
|