How do you sample from a probability distribution?

A model predicts P(a)=0.6P(a)=0.6, P(b)=0.3P(b)=0.3, and P(c)=0.1P(c)=0.1. These numbers describe a distribution. Sampling selects an actual outcome according to those probabilities. It can choose b or c even though a is most likely. Always choosing a would be a different procedure: taking the most probable outcome, or argmax.

Turn probabilities into intervals

Divide the interval from 0 to 1 into lengths matching the probabilities:

CharacterProbabilityCumulative probabilityUniform values selecting it
a0.60.60≤u<0.60\le u<0.6
b0.30.90.6≤u<0.90.6\le u<0.9
c0.11.00.9≤u<10.9\le u<1

Draw a uniform number uu from [0,1)[0,1). Uniform means equal-length intervals have equal probability. Selecting the interval containing uu gives each category its intended probability, because its interval has exactly that length.

Before running, predict which character the manually inspected value 0.73 will select and why. Use the reference settings: probabilities 60%, 30%, 10%; 10 draws; seed 42; inspected value 0.73. Record the run.

Now predict and change only Inspect one uniform value to 0.6. Does this change the recorded random counts, or only the separate probe?

Sample with replacement from a fixed categorical distribution. Each run restarts a reproducible teaching generator at the selected seed: s ← (1664525s + 1013904223) mod 2^32; u = s / 2^32. Select the first cumulative probability strictly greater than u. More draws with the same seed extend the same sequence. The inspected value is a separate manual probe, not an extra random draw.

0/16 runs. Each run keeps its own inputs and results. Work stays in this tab across reloads; download a report before closing it.

Write a prediction to record your reference.

Compare the interval picture and selected character before opening the explanation. The probe is a chosen test input, not a random draw; it never enters the counts. Open Inspect calculated records to see the uniform inputs used for the random draws.

Check the selections and boundaries

Both 0.73 and 0.6 select b. We include the left endpoint and exclude the right: [0.6,0.9)[0.6,0.9). A value of 0.9 selects c. Changing only the probe leaves the generated draws and their counts unchanged.

This convention also handles zero probability: an interval of length zero cannot contain a draw.

More draws do not mean exact quotas

Return to Reference settings and select the reference run as your baseline. Predict whether 10 draws must contain exactly six a’s, three b’s, and one c. Record those counts and their observed percentages.

Change only Number of draws to 100, then 1000, predicting before each run. Keep probabilities and seed fixed. Every run restarts the same generator, so the longer runs contain the shorter run as their prefix. These are nested comparisons, not independent repeated experiments.

Compare expected and observed percentages. Did every category move closer to its probability at every step? Explain before opening the feedback.

Interpret the frequency comparison

The expected count of a in nn independent draws is 0.6n0.6n. An expectation is an average over hypothetical repetitions, not a requirement for one run. In the ideal model, even ten a’s in a row have nonzero probability, 0.6100.6^{10}.

Under independent sampling from a fixed distribution, observed proportions converge to the probabilities as the sample size grows. They can fluctuate toward and away from the target along the way. A single comparison is evidence about these particular draws, not a proof of that convergence law.

Our browser uses a small, seeded pseudorandom generator for inspectable, repeatable evidence. It approximates the uniform-draw idea; its finite deterministic sequence is not an infinite sequence of truly independent random variables.

Cumulative probabilities and the inverse CDF

For a numerically ordered random variable, the cumulative distribution function is F(x)=P(X≤x)F(x)=P(X\le x). It accumulates probability up to xx. For categorical sampling, choose a fixed list order a, b, c; this is a bookkeeping order, not a numerical meaning of the characters. The cumulative array is [0.6,0.9,1][0.6,0.9,1].

Our implementation chooses the first cumulative entry strictly greater than u∈[0,1)u\in[0,1). Mathematically, inverse transform sampling is often written using the generalized inverse

F−1(u)=inf⁡{x:F(x)≥u},0<u<1.F^{-1}(u)=\inf\{x:F(x)\ge u\},\qquad 0<u<1.

With an ideal continuous uniform draw, hitting an exact boundary has probability zero, so the boundary conventions give the same distribution. Computer generators have finite precision; define and test the convention you implement. The strict comparison matches our half-open intervals and skips leading zero-probability categories even at u=0u=0.

Read the PyTorch operations

Here is an explicit implementation for the three fixed probabilities:

import torch

g = torch.Generator(device="cpu").manual_seed(42)
p = torch.tensor([0.6, 0.3, 0.1], dtype=torch.float64)
p = p / p.sum()
cdf = torch.cumsum(p, dim=0)
cdf[-1] = 1.0  # Ensure the final boundary is exactly one.
u = torch.rand(10, generator=g, dtype=p.dtype)
indices = torch.searchsorted(cdf, u, side="right")
characters = ["abc"[i] for i in indices.tolist()]
print(characters)

torch.cumsum constructs cumulative totals. torch.rand supplies uniform values in [0,1)[0,1). torch.searchsorted(..., side="right") locates the first boundary greater than each value. Its default left-side search uses a different equality rule.

For ordinary categorical draws, PyTorch provides a direct operation:

g = torch.Generator(device="cpu").manual_seed(42)
indices = torch.multinomial(p, 10, replacement=True, generator=g)
characters = ["abc"[i] for i in indices.tolist()]

torch.multinomial returns sampled indices. Its input can also be unnormalized finite, nonnegative weights with a positive sum. Use replacement=True here so categories can recur; the default is sampling without replacement. It need not use our inverse-CDF algorithm internally, so matching a seed does not imply matching the explicit implementation’s sequence.

In text generation, the model produces logits, softmax turns them into probabilities, and a sampler selects the next character or token. The next context usually changes the next distribution. Our fixed-distribution experiment isolates just the sampling step. See random seeds in PyTorch for resetting, advancing, and isolating generator state.

Sampling continuous values

A continuous distribution has probability over intervals. If it has density ff, then

P(a≤X≤b)=∫abf(x) dx.P(a\le X\le b)=\int_a^b f(x)\,dx.

The curve’s height is a density, not the probability of that exact point; a single point has probability zero for an absolutely continuous distribution. The CDF still accumulates probability, now as area. Inverse transform sampling uses X=F−1(U)X=F^{-1}(U) when an inverse is available.

For a uniform value between 2 and 5, transform UU to X=2+3UX=2+3U. For an exponential waiting time with rate λ>0\lambda>0, F(x)=1−e−λxF(x)=1-e^{-\lambda x} for x≥0x\ge0. Solving u=F(x)u=F(x) gives

X=−ln⁡(1−U)λ,U∈[0,1).X=-\frac{\ln(1-U)}{\lambda},\qquad U\in[0,1).

For example, with U=0.5U=0.5 and λ=2\lambda=2, the sampled value is ln⁡2/2≈0.347\ln2/2\approx0.347. For numerical computation, -log1p(-u) / rate avoids loss of precision near zero.

Not every inverse CDF has a convenient formula. Numerical inversion approximates it; rejection sampling accepts or rejects proposals using a suitable bound. These are among the methods described in SciPy’s sampling tutorial. Specialized normal samplers include Box–Muller, while alias tables accelerate repeated categorical draws. For more complicated targets, MCMC builds a dependent chain whose limiting distribution is the target under appropriate conditions; it is not simply a way to obtain immediate independent samples.

Design your own sampling comparison

Choose another probability preset. Before running it, predict which of the reference uniform inputs will switch characters and why. Keep seed, draw count, and probe fixed. Compare the individual mappings and counts against the matching reference.

Then design a boundary test using the preset with probability zero for a. Choose a probe that would expose an incorrect selection of a. What result would contradict the claim that empty intervals are never selected?

Check the comparison

Changing probabilities moves boundaries without changing the uniform inputs. A draw switches characters only if its interval assignment changes. With probabilities [0,0.7,0.3][0,0.7,0.3], even u=0u=0 selects b: the first boundary strictly above it is 0.7. No draw should select a.

For transfer, choose your own continuous uniform interval [a,b)[a,b) with a<ba<b. Write a transformation of UU, predict where U=0.25U=0.25 lands, and check that an interval covering one quarter of the output range gets probability one quarter. The check is X=a+(b−a)UX=a+(b-a)U; you choose the endpoints.

Download your report. Keep probabilities, uniform inputs, observed outcomes, and seed distinct in your explanation.

Continue

Previous: Mean and variance · Next: Random seeds in PyTorch · All statistics notes