Numerical Python·Lesson 25 of 67·intermediate
Random number generation and seeding
The modern Generator API, why a global seed is a trap, and how to sample tokens from a distribution with a temperature — reproducibly.
In this lesson · 16 min
- →Create a seeded Generator with default_rng and draw from it
- →Explain why a seeded run reproduces and an unseeded one cannot
- →Keep independent streams independent instead of sharing a global one
- →Shuffle and split a dataset reproducibly
- →Sample from a probability distribution, including temperature sampling
Randomness is everywhere in machine learning: the weights you start from, the order you see examples in, the split between train and test, the token a model picks. Controlling it is what separates a result you can defend from one you cannot repeat.
The Generator API
The seed fixes the entire stream. Two generators made from the same seed produce identical sequences, for ever:
Independent streams
When several parts of a pipeline need randomness, give each its own generator rather than
interleaving draws from one. spawn derives children that are statistically independent and
still fully determined by the parent seed:
Distributions
Weighted choice draws from a distribution you supply:
Shuffling and splitting a dataset
Shuffle indices, not the data, so several parallel arrays stay aligned:
Sampling with a temperature
This is how a language model picks its next token. The logits become probabilities through a softmax, and temperature controls how sharp that distribution is:
Low temperature concentrates the mass on the top token and the output becomes nearly deterministic; high temperature flattens it and unlikely tokens start appearing.
Greedy decoding is the limit as temperature approaches zero — just argmax, no sampling at all:
Masking with -inf works because np.exp(-inf) is exactly 0, so those tokens can never be
drawn.
Try it yourself
Split the ten examples into an 80/20 train and test set using a generator seeded with 7. Shuffle the indices so features and labels stay aligned.
Write sample(logits, temperature, rng): apply the temperature, turn the logits into probabilities with a stable softmax, and return one index drawn from that distribution.
Check yourself
Why prefer np.random.default_rng(seed) over np.random.seed(seed)?
Check yourself
What does raising the sampling temperature from 0.5 to 2.0 do to the token distribution?
Recap
np.random.default_rng(seed)gives a private, seeded generator. Pass it around; do not reach for the globalnp.randomfunctions.- The same seed reproduces the same stream exactly — which is what makes a reported score checkable.
parent.spawn(n)derives independent child streams from one seed.- Shuffle indices and apply the same order to every parallel array, so rows keep their labels.
- A temperature divides the logits before the softmax: low sharpens towards greedy, high
flattens. Masking with
-infremoves a token entirely, becauseexp(-inf)is 0.