liveBook · Manning

Pseudo-random number generator

As explained in Deep Learning with Python, Third Edition

A pseudorandom number generator is a deterministic computational process that uses an initial seed or internal state to produce a sequence with random-like statistical properties.

A pseudorandom number generator produces random-like values from a seed or internal state. In JAX, random operations use explicit keys rather than hidden global state, so repeating a call with the same key returns the same value. section 3.5.3

To manage random values in JAX:

  • Create a key from an integer seed and pass it as the first argument to random operations. section 3.5.3
  • Split an existing key to get a different key when you need new random values; the same sequence of splits is deterministic. section 3.5.3
  • Manage keys outside functions that need fresh values. This takes more work than relying on NumPy’s global state, but supports deterministic, vectorizable JAX code. section 3.5.3
Listing 3.30 Using a random seed in Jax
>>> seed_key = jax.random.key(123)
>>> jax.random.normal(seed_key, shape=(3,))
Array([-0.1470326,  0.5524756,  1.648498 ], dtype=float32)
>>> jax.random.normal(seed_key, shape=(3,))
Array([-0.1470326,  0.5524756,  1.648498 ], dtype=float32)

The repeated calls use the same key and produce identical random tensors. section 3.5.3

How Deep Learning with Python, Third Edition covers it

How deep it goes
Explains key creation, repeatable calls, and deterministic splitting without detailing the generator algorithm.
Examples
Demonstrates repeated random.normal calls with one key and a new output after splitting that key.
What it assumes
Assumes familiarity with random calls such as NumPy's and explains hidden global state before introducing explicit JAX keys.
What you can do after
Apply it via jax.random.key and jax.random.split to produce repeatable random tensors without hidden global state.
How it connects
Connects JAX random operations with NumPy's global random state, statelessness, determinism, and vectorization.

Also explained in

Compare how 10 books explain pseudo-random number generator →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.