A4 · Lesson 49 · Random numbers and Monte Carlo
Build ten thousand boards before you build one
A filter's corner depends on a resistor and a capacitor, each a little off from its marked value in a random way. The worst case (both off in the bad direction) is easy to compute and rarely happens. What a manufacturer needs to know is how often a board will be out of spec. Simulate ten thousand boards, each with its own randomly drawn parts, and count. That's the Monte Carlo method, and with NumPy it's a few lines.
Predict
A Monte Carlo yield estimate from 1000 simulated boards is 95%. Roughly how uncertain is that estimate?
About ±0.7%. Each board passes or fails, so the estimate's standard error is . Monte Carlo results are themselves random, with error shrinking as : four times the boards for half the uncertainty.
Name it
Random numbers in NumPy
rng = np.random.default_rng(42) # a seeded generator
rng.uniform(0.95, 1.05, size=10000) # flat between two limits
rng.normal(1.0, 0.0167, size=10000) # bell-shaped: mean, standard deviation
Always use a seeded generator for anything you'll check or report, so it can be repeated exactly.
Modeling tolerances
A part marked ±5% can be modeled two ways:
- uniform across the band, a cautious choice when you know nothing else;
- normal with the band at (so = tolerance/3), closer to what production lines often deliver.
Which you choose changes the tails of the result, which is where yield is decided, so say which one you used.
The method
- Draw every uncertain input as an array of samples.
- Compute the output for all at once, with array arithmetic.
- Summarize: the mean and standard deviation, the fraction within spec (the yield), percentiles with
np.percentile.
N = 100_000
R = 10e3 * rng.normal(1, 0.05 / 3, N)
C = 10e-9 * rng.normal(1, 0.10 / 3, N)
fc = 1 / (2 * np.pi * R * C)
yield_ = np.mean(np.abs(fc / 1591.5 - 1) < 0.05)
Accuracy
The standard error of a Monte Carlo mean is , and of a yield . Run with enough samples that this is small compared with the decision you're making. Rare failures (one in a million) need special techniques, since plain sampling would need hundreds of millions of draws to see a few.
Monte Carlo against worst case
Worst-case analysis gives guaranteed bounds, but the bounds are usually far outside where real boards land. Monte Carlo gives the realistic spread but no guarantee. Engineers use worst case for safety-critical limits and Monte Carlo for cost and yield decisions.
Worked example
A divider uses two 10 kΩ ±1% resistors, modeled as uniform. Compare the Monte Carlo spread of the ratio with the worst case.
Show the solution
- Simulate 100,000 boards. Draw each resistor uniformly within ±1% and compute the ratio.
import numpy as np
rng = np.random.default_rng(1)
N = 100_000
top = 10e3 * rng.uniform(0.99, 1.01, N)
bot = 10e3 * rng.uniform(0.99, 1.01, N)
r = bot / (top + bot)
print(r.std(), r.min(), r.max()) # about 0.00204, 0.4950, 0.5050
- The worst case. which is ±1%.
- The Monte Carlo spread. Out of 100,000 simulated boards a few come close to the worst case, but the standard deviation is only 0.4% of the ratio and about 95% of boards are within ±0.8%.
- The shape. The ratio's distribution is nearly triangular, peaked at 0.5, because to first order it depends on the difference of two flat errors.
Answer the worst case is ±1%, but the Monte Carlo standard deviation is only 0.4%, and about 95% of boards fall within ±0.8%.
Worked example
Estimate by throwing random points at a square, and watch the error shrink.
Show the solution
- Throw points at the square. The fraction landing inside the unit circle is , so four times it estimates .
for N in (100, 10_000, 1_000_000):
x, y = rng.uniform(-1, 1, (2, N))
print(N, 4 * np.mean(x**2 + y**2 <= 1))
- Watch the error. Each hundredfold increase in makes the typical error about ten times smaller, from a few percent to a few hundredths of a percent.
Answer four times the fraction inside the circle estimates , and the error falls about tenfold for each hundredfold increase in .
Check
With rng = np.random.default_rng(s) and N = 100000, draw top and bottom resistors of a 10 kΩ/10 kΩ divider as uniform within ±t = 1%, top first. Set r to the array of ratios, sd to their standard deviation, and frac to the fraction within ±t/2 % of 0.5.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Model R = 10 kΩ ±tR% and C = 10 nF ±tC% as normal with = tolerance/3. With rng = np.random.default_rng(0) and 200,000 draws (R first), set fc to the corner frequencies and yld to the fraction within ±spec% of the nominal corner.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A Monte Carlo run of N = 1000 boards finds a yield of p = 0.95. Set se to the standard error of that estimate, and N4 to how many boards would halve it.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Estimate with rng = np.random.default_rng(s) and 1,000,000 points drawn as x, y = rng.uniform(-1, 1, (2, N)). Set pi_est and err to its absolute error.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
An LED driver has . Model 100,000 boards with rng = np.random.default_rng(s), drawing in this order: Vs normal with mean 5 V and = 0.05 V, Vf normal with mean 2.0 V and = 0.05 V, and R uniform within ±tR = 1% of 150 Ω. Set I to the currents, yield_ to the fraction within 18 to 22 mA, and worst to the largest current.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A Monte Carlo yield estimate near 0.95 has a standard error of . How many simulated boards give a standard error of 0.5%?
Explain why
When would you trust a worst-case analysis over a Monte Carlo simulation, and when the reverse?
Worst case gives a guaranteed bound: if every part is within its tolerance, the output can't be outside it. That's what you want for safety limits or anything that must never fail. But it assumes every part sits at its extreme in the worst direction, which almost never happens, so it overstates the real spread. Monte Carlo shows how outputs are actually distributed and how often a design would fail, which is what matters for yield and cost decisions, but it's only as good as the tolerance distributions you assume and gives no guarantee about rare cases.
Practice
Keep practicing
8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.