liveBook · Manning

Pseudo-random number generator

As explained in Deep Learning with R, Third Edition

A pseudo-random number generation operation deterministically produces values that appear random from an algorithmic seed or key.

A pseudo-random number generator (PRNG) deterministically produces values that appear random from a seed. Stateless systems require the seed or key to be passed explicitly instead of hiding it in global state. section 3.5.3

The key implementation ideas are:

  • R and NumPy-style calls can depend on hidden global generator state; setting a seed makes repeated operations reproducible. section 3.5.3
  • JAX uses explicit PRNG key objects, passed as the first argument to random operations. section 3.5.3
  • Reuse a key to reproduce the same result; split it to obtain a new key and different random values. section 3.5.3

Reproducible noise and JAX values

An R function can set a supplied seed before adding uniform noise, so identical inputs and seeds produce identical outputs. In JAX, create a key with jax$random$key(123L); repeated normal calls with that key match, while jax$random$split() supplies a new key for different values. section 3.5.3

seed_key <- jax$random$key(123L)
jax$random$normal(seed_key, shape = shape(3))

Array([1.6359469 , 0.8408094 , 0.02212393], dtype = float32)

new_seed_key <- jax$random$split(seed_key, num = 1L)@py[0]  #1
jax$random$normal(new_seed_key, shape = shape(3))

Array([-0.49093357, -0.9478693 , -1.775197  ], dtype = float32)

Inspect the complete JAX key-reuse and key-splitting example. section 3.5.3

How Deep Learning with R, Third Edition covers it

How deep it goes
Explains global state, explicit keys, reproducibility, and key splitting through runnable implementation examples.
Examples
Uses R uniform-noise generation, JAX normal sampling, repeated keys, and split keys.
What you can do after
Apply explicit seeds and JAX keys to reproduce results and generate fresh random values predictably.
How it connects
Connects random seeds with R, NumPy, Python, TensorFlow, Torch, Keras, JAX, and stateless programming.

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 R, Third Edition. The section links point to the parts of the book it draws on.