Step 1 · One blind rule

Diffusion, one grain at a time

Course 1 followed individual birds, each steering by rules. This course keeps following individuals but replaces the steering with chance, and looks at what a crowd of them does. It is the shortest route to the equations of Courses 3–5.

The lab from the foot of this page, running live: 16,000 grains, one rule each.

One blind rule, and a crowd with a shape

A random walker follows one rule: every tick, take one step in a random direction. It has no memory, no plan, and no knowledge of the other walkers. Release a few thousand and count how many sit in each patch of space, and that count spreads exactly as the heat equation says a temperature curve does. The rule is in the grain; the equation is in the count.

That is the difference between this course and the three after it. Courses 3–5 divide space into fixed cells and store one number in each — how much stuff is here — then update those numbers as time passes. The cell stays put; its contents rise and fall. A set of numbers laid out over space like that is called a field, and the approach is called Eulerian. Here we do the reverse: pick a grain and record where it goes, which is Lagrangian. Both names come up in any simulation work, and the rest of this section compares what the two can express.

The two don't agree exactly. The equation describes the average crowd — the one you would get from infinitely many walkers — so a real crowd's count in each patch sits a little above or below it. That gap has a size, and the same number explains why you are not visibly jittering right now.

Where does the square root come from? From a random walk, one level up. Take any object being struck n times in a tick, each hit landing either on its left flank or its right:

  • Score +1 for a hit on the right, −1 for one on the left, and keep a running total. Perfect balance would be a total of zero.
  • That running total is itself a walk of n steps of size 1 — it goes up or down by one with each hit, at random.
  • A walk of n unit steps typically finishes about √n away from where it started. (That's the result derived further down this page, with the step size set to 1.)
  • So after n hits the two flanks differ by about √n. As a fraction of all n hits, that is √n / n — which is 1/√n.

With numbers: 100 hits give a typical mismatch of about 10, which is 10% of the total. 10,000 hits give a mismatch of about 100 — ten times larger — but that is only 1% of the total. The mismatch grows; its share shrinks. Chance never cancels out, it just gets outvoted.

Which is why size decides who feels it. Below, two objects sit in the same bath of molecules, taking hits under the same rule. The large one presents a big target and takes well over a thousand hits a second; its two flanks differ by only a percent or so of that, which is why it barely moves. The small one takes a couple of hundred, its flanks differ by around three percent, and it visibly wanders. The readout gives each mismatch as a multiple of √n — and that multiplier comes out near the same value for both objects, which is the law itself.

Why big things sit still amber arrow = the net push right now · readout = hits per second and the mismatch between the two flanks · same rule for both objects, different hit counts

Now switch back to the Eulerian description and ask what it can say about this. It has one number per cell — that is all a field is — and one number is an average. There is nowhere in it to also write "and this figure wobbles by 3%." So the mismatch we just measured is not something the field gets wrong; it is something the field has no way of holding. The Lagrangian description has it for free, because it keeps every grain separately.

Most of the time that costs nothing. At ordinary concentrations n per cell is astronomically large, the wobble is far too small to notice, and the field is indistinguishable from the truth — which is why Courses 3–5 can ignore all of this and still be right. It only matters when n is small, and then the graininess is measurable. That case has a name.

Probability in, calculus out

Courses 3–5 are calculus from start to finish: rates of change, derivatives, partial differential equations. This page uses calculus too, but it does not start there. It starts in probability — random variables, averages, variance — and arrives at calculus a few lines in. The order is the point, so it's worth knowing which tool is doing what.

Probability supplies two things. First, the rule for one grain: a step of random direction, with no memory. Second, the statistics of crowds — the law of large numbers, which is why a crowd has a definite shape when no individual walker does, and the central limit theorem, which says that shape is a bell curve, since adding up many independent steps always produces one. The 1/√n figure above comes from the same place.

Calculus enters the moment you stop asking about a grain and ask about the average. Two lines into the derivation below, the bookkeeping produces the average of a point's two neighbours, minus the point itself — and that is a second derivative, written in differences instead of symbols. Nobody put it there; it fell out of counting. The limit that follows only tidies it into the notation Courses 3–5 use.

So this is a conversion between the two, not a choice between them. Probability describes what one grain does; calculus describes what the average of very many of them does. This page is where one becomes the other, which is also why the same equation can be solved on a grid in Course 3 and sampled with grains here and give the same answer.

Brownian motion

In 1827 the botanist Robert Brown watched pollen of Clarkia pulchella under a microscope and saw that the small particles inside the grains would not sit still: they jittered continuously and went nowhere. To test whether this was a sign of life he tried inorganic samples — soot, powdered glass, a chip of stone from the Sphinx. Those jittered too. No mechanism was proposed for the next eighty years.

In 1905 Einstein explained it, and the explanation rests on what temperature actually is. Water is made of molecules, and they are never still: what a thermometer measures is how fast they are moving, and only at absolute zero would they stop. At room temperature a water molecule travels at several hundred metres per second between collisions and hits a neighbour something like a trillion times a second. That ceaseless movement is what "thermal motion" means — heat and molecular motion are not two things linked by cause, they are the same thing described two ways.

So a grain suspended in water is being struck from every direction, constantly. A large object takes so many hits that they cancel and it stays put; a small one takes a slightly uneven count and moves. That is the 1/√n imbalance from the section above, at a scale visible through a lens.

He also made it quantitative. The prediction is that a grain's mean squared displacement grows in proportion to time: ⟨x²⟩ = 2Dt. Two pieces of that need unpacking, starting with the brackets — ⟨ ⟩ means "averaged over the whole crowd." It is not one grain's distance, it's the average of the squared distances of all of them.

Why squared, and why proportional to t? Because the steps are independent, and independent steps don't accumulate their lengths — they accumulate their squares. Half go one way and half the other, so the average displacement stays zero forever; after a million steps a grain is, on average, exactly where it started. The average of the square can't cancel that way, because squaring throws away the sign: every step contributes h² whichever way it went. So N steps contribute N·h², and with N = t/Δt steps in time t that is (h²/Δt)·t — a constant times t. The constant is the D from before.

So distance² keeps pace with time, and distance itself grows like √t. To wander twice as far takes four times as long. Below, a crowd released from a single point, next to a live plot of its mean squared displacement — a straight line, tracking the prediction.

Distance² against time left = the crowd spreading, dashed ring = √(4Dt) · right = measured mean squared displacement (teal) against the prediction 4Dt (amber) — 4, not 2, because this cloud spreads in two dimensions

Marian Smoluchowski derived the same result independently in 1906. Then between 1908 and 1911 Jean Perrin tracked real grains under a microscope and measured their spread against this formula.

That measurement settled the atom question, and the mechanism matters, because explaining the jitter alone would not have done it. Einstein's expression for D contains Avogadro's number — the count of molecules in a mole — alongside the temperature, the fluid's viscosity and the grain's radius. Those three you can measure with a thermometer, a viscometer and a microscope. So rearranging it turns a measurement of something visible, the wandering of a speck you can watch, into a count of something invisible.

Perrin did that and got roughly 6×10²³ per mole, in agreement with values obtained from completely unrelated experiments. Two things followed. A genuinely continuous fluid would push on a grain perfectly evenly and produce no jitter at all, so the jitter is direct evidence that the fluid comes in discrete pieces. And because the count matched what chemists had arrived at by other routes, those pieces had to be the same objects chemistry was already counting. Perrin received the Nobel Prize in 1926.

The walkers below are Brownian motion rather than an analogy for it: Δx = v·Δt + √(2D·Δt)·ξ is its standard description, formalized by Norbert Wiener in the 1920s — hence Wiener process for the idealized version. The same process prices stock options, models pollutant plumes, and, run backwards, generates images in diffusion models.

Step 2 · The derivation

From coin flips to the heat equation

Four equations get from a coin flip to the heat equation. Hover any coloured term for its definition, or use the full list at the end of the section.

Two of the symbols coming up are ours, not nature's. Nothing in water moves in fixed-size hops on a fixed clock — we imposed a stride h and a tick Δt so there would be something countable. They are dials we set, and more than one setting describes the same spreading: halve both and you get a different pair of numbers for identical physics. So an equation still containing h or Δt would be partly a statement about our own bookkeeping. The target is an equation in ρ, x and t alone. Watch for those two disappearing.

There is a concrete payoff for getting rid of them. Course 3's heat equation is ∂T/∂t = α·∂²T/∂x² — no stride anywhere in it, no tick, nothing but derivatives of a smooth field. So while our version still says h and Δt, the two cannot even be set side by side: there is no symbol in the heat equation for ours to correspond to. Strip them out and the resemblance stops being a matter of opinion — the equations turn out to have the same form, with the one constant that survives from the walk standing exactly where α stands. A rod's temperature and a crowd of grains obey one equation, which is not obvious and is the reason for doing this at all.

One more thing about the setup: the tank at the end of this course is a square, and grains there move in two directions. The derivation below runs on a line — one dimension, where a grain has exactly two places to go. That is purely for legibility; the two-dimensional version is the same argument with four neighbours instead of two, and it produces the same equation. The only visible consequence is a factor: the typical squared distance grows as 2Dt on a line and 4Dt on a plane, which is why the figure further down is labelled 4Dt while the formula here says 2Dt.

Line by line

Start by counting. A walker moves exactly one stride per tick, so anyone standing at x a moment from now has to be standing at x−h or x+h right now — nowhere else is close enough to reach. Each of those two groups then splits evenly: half of them step left, half step right. So the crowd that arrives at x is half the group from the left plus half the group from the right:

½ ½ ½ ½ lands x−h x x+h one stride, h
Only two places are within one stride of x. Each of those groups splits evenly — half steps toward x (solid), half steps away (dashed) — so what lands at x is half of one group plus half of the other.

Step one · who can possibly be here next tick

The crowd at x one tick from now is half the crowd one stride left plus half the crowd one stride right. Those arrivals are the only way to be at x, so they account for all of it.

That counts grains: it says how many are at x. The heat equation instead describes how a crowd changes, so the same fact needs restating as a change — by taking away the crowd that was already there:

Step two · the same statement, as a change

In one tick the crowd here changes by the average of its two neighbours, minus itself. Same statement as the line above, rearranged.

That bracket is a second derivative — written in differences rather than symbols, but the same quantity Courses 3 and 4 write as ∂²ρ/∂x². This is the moment the probability becomes calculus, and it arrived by counting. Read it as a curvature meter: if your two neighbours average to exactly your own height you are on a straight line, the bracket is zero, and nothing changes — even if the line is steep. If they average higher you are in a dip and the crowd here grows; lower, and it drains.

Δρ is not yet a rate. It is the change during one tick — an amount, whose size depends entirely on how long the tick was. Halve the tick and Δρ halves with it. Dividing by Δt gives change per second, which comes out roughly the same whichever tick was chosen; that independence is what makes it a fact about the physics rather than about the bookkeeping.

That division is also where time becomes a variable. Until now it has been a step counter — tick one, tick two — and a walker existed only at those instants, with nothing defined in between. Dividing by Δt and then shrinking it makes time continuous, so ρ has a value at every moment rather than only on the ticks. ∂ρ/∂t means nothing until that has happened. Space gets the same treatment: the stride made positions discrete, and shrinking h fills in the gaps.

In full, three lines:

Step three · dividing through, and the heat equation appears

The last line reads: how fast the crowd at x changes in time equals D times how sharply the density profile bends at x — ρ drawn as a curve against position. That is Course 3's heat equation, reached from coin flips. Hover a symbol above to recall it.

Line one rewrites the bracket. A discrete second derivative is [ρ(x−h) − 2ρ(x) + ρ(x+h)] / h² — that's the standard definition — and our bracket is exactly half of that numerator, so the bracket equals (h²/2)·∂²ρ/∂x². Line two divides everything by the tick, because Δρ was the change during one tick and we want change per second. Line three is that same equation with the left-hand quantity written as a derivative.

Which is worth naming plainly, because it is the only piece of calculus in the whole derivation. A derivative is the limit of (change ÷ interval) as the interval shrinks to zero — that is its definition, and "the slope" is what that limit computes. So ∂ρ/∂t does not mean anything more exotic than: form Δρ/Δt, then let Δt go to zero. Line two forms it; letting the tick shrink is what makes line three a derivative rather than a ratio.

The same definition, applied twice in space instead of once in time, is what turns a difference of differences into ∂²ρ/∂x² as h goes to zero. That is the entire calculus content here: one definition, used on time once and on space twice. Everything else was counting.

Now look at what happened to h and Δt: after line two they never appear apart again — only ever in the single combination h²/2Δt. That is what "collapse into one constant" means. Two details of the walk go in, one number comes out, and nothing else about the stepping survives — which is why that one number gets a name.

D is the diffusion coefficient: one number for how fast this stuff spreads. It isn't a new ingredient — it's the stride squared over twice the tick, so a jumpier grain gives a larger D and a faster-spreading crowd. It plays the same role as α in Course 3 and Dᵤ, Dᵥ in Course 4, and it is the jitter slider in the lab below.

It also settles why the stride (h) and the tick (Δt) have to shrink together. Since D = h²/2Δt, the two can be shrunk at any relative rate you like, and only one choice keeps D finite. The three panels below are exactly those three choices, refined side by side and left running for the same length of time:

  • Tick shrinks like the stride — halve one, halve the other. Then D = h²/2Δt halves too, and keeps halving: the crowd stops spreading and freezes. Left panel: the cloud collapses toward a dot.
  • Tick shrinks like the stride squared — the one pairing that holds D fixed, since the h² on top and the Δt underneath shrink in step. Middle panel: the cloud keeps the same size however fine the steps get. This is the case that survives, and the motion it describes is Brownian motion — perpetual jittering with no preferred direction, spreading at a definite rate.
  • Tick shrinks like the stride cubed — the tick collapses far faster, so D grows without bound and the crowd spreads infinitely fast, reaching everywhere at once. Right panel: the cloud floods its box.

Watch the D readout under each panel as the stride divides down: only the middle one holds still.

What happens when they don't shrink together the stride shrinks continuously in all three · they differ only in how fast the tick shrinks with it · same elapsed time throughout

That is the crowd accounted for. Step four, below, is the last equation and a different kind of statement: not the crowd, but a single grain in a single tick — and it is the line the simulator actually runs. It adds two contributions, only one of which has appeared so far.

The jitter — its size set by D — is the part we have had all along: a step of typical size √(2DΔt) in a direction chosen by chance, different for every grain. That alone is pure diffusion, and it is what every walker on this page has been doing so far. Drift — written v, and not to be confused with D — is new: a steady push in one direction that every grain shares equally, such as a current in the water or gravity on a speck heavy enough to feel it. Its proper name is advection. With v at zero you are back to pure diffusion, so the equation below is just the general case with both terms present:

Step four · one grain, one tick

A grain's move in one tick is drift (v) plus jitter (sized by D). The drift term is identical for every grain; the jitter term is drawn afresh for each one. With v at zero it's pure diffusion; with D at zero every grain marches in identical lockstep.

Both are knobs in the lab: v sets the drift — the same quantity Course 4's tank controls on its walls — and D sets the size of the jitter.

If D varies from place to place, a grain uses whatever value holds where it stands; no extra rule is needed. The consequence is that grains dawdle where the jitter is weak, so the crowd piles up on slow ground — structure from one species with no interaction between grains. Transport codes usually add a correction to cancel that pile-up; this lab keeps it, since it's what the ground-painting is for.

Every symbol, once

ρ
density — how crowded it is at one spot: grains per unit of length. The tall middle of the curve means "lots of grains here."
x
position — where along the line we're looking.
t
time — the clock reading right now.
Δt
one tick — the gap between one step and the next. "Δ" just means "a small amount of," so Δt is a small amount of time.
h
the stride — how far a single blind step carries a grain.
D
the diffusion coefficient — how vigorously this stuff spreads, built from the walk itself as h² ⁄ 2Δt. It sets the size of the jitter, and it is the lab's jitter slider.
v
drift — a steady push every grain shares equally, i.e. a current. Zero means still water. Also called advection.
ξ
the random part of the jitter — a fresh number every tick, centred on zero and typically about ±1, equally likely either way. The coin flip, grown up: this is the only place chance enters the model.
∂ρ/∂t
rate of change of density in time — stand still at one spot, watch the clock, and see how fast the crowd there grows or thins. (Course 3 introduces this ∂ notation in full.)
∂²ρ/∂x²
how sharply the density curve bends in space — the curvature meter: me, versus the average of my neighbors.

The whole simulator, one line per axis

This is the entire simulator below — no grid for the physics, no Laplacian, no stencil. Keep it in mind for Course 3, where the same physics is computed the other way round — dT[i] = α·((T[i−1]+T[i+1])/2 − T[i]), a loop over fixed places. Same equation; that one updates amounts at fixed places, this one updates places of fixed grains.

What each way costs

The two bookkeepings don't just look different, they cost differently, and the difference decides what can run on a small device. This loop costs one pass per grain: with N grains it's N updates a frame, plus a fresh random number per grain per axis, and memory grows with N. Course 3 and Course 4 cost one pass per cell, and that price doesn't move — a cell holding five thousand grains' worth of density costs exactly what a cell holding three costs, because it's storing a number, not a crowd.

Two consequences worth knowing. First, random numbers are the expensive part here: 16,000 grains × 2 axes is 32,000 draws a frame, and on a microcontroller that, not the arithmetic, is what sets the frame rate. Second, and worse for hardware: a stencil pass reads the same relative offsets at every cell, so the addresses are predictable and every cell can be computed independently. Grains do the opposite — each one writes wherever it happens to be, so drawing them means scattered writes into the canvas, and two grains landing in the same place collide if you try to parallelize.

Which is why Course 4's tank runs happily on a chip at a resolution this lab can't touch, and why putting grains on that chip means either accepting a much smaller N, or going back to a grid — keeping an integer count per cell and splitting each cell's count randomly among its neighbors. That version costs the same as the stencil, keeps the noise a smooth density loses, and gives up the one thing this loop is for: the ability to follow a single grain.

Step 3 · The lab

Hidden ground, walls, and a crystal

16 000 grains, live. Two regimes. Sealed dish: walls reflect, nothing is born or dies — a drop of ink spreading; add drift and the floor turns it into an atmosphere, dense below, thin above. Flow-through: grains rain in across the whole top wall, ride the drift down, and die at rate k — a steady plume, the textbook picture of a contaminant leaking into groundwater.

In both, give the crowd something to reckon with — stamp a shape or draw your own, in either of two flavours. Slow ground lets grains in but makes them dawdle, so the shape fills in denser — a solid, and it needs only patience. A wall keeps them out entirely, so they crowd its rim and stream around it; turn the drift up and that's a sheet caught on a statue, an outline instead of a fill. (Without a current the crowd settles evenly around a wall and you get a silhouette-shaped hole — pretty, but no rim. A rim needs something pressing against the obstacle.)

A third regime adds one rule to the walk. Sticky · DLA: wander freely, but the moment you touch the cluster you join it permanently. This is diffusion-limited aggregation, and it grows a Brownian tree — the branching shape of frost on a window, copper plating out of solution, a mineral dendrite in rock, a lightning channel. Its edge is a fractal of dimension about 1.71, so it is neither a line nor a solid.

Branches rather than a blob, because of screening. A wanderer arriving from outside has to get past the protruding tips to reach the interior, and rarely does, so tips grow faster, which makes them better at catching the next arrival. The interior is starved. (This is why the lab runs only a few hundred walkers in this mode: with all 24,000 loose, every cell beside the cluster is hit at once, screening never happens, and the result is a solid disc.)

Each new arrival takes its colour from the cell it stuck to, plus a small random offset, so hue drifts along a branch as it extends: limbs that separated early end up different colours, neighbours stay similar. Two things to try — add drift for one-sided, wind-blown growth, and stamp a wall shape first, since the crystal grows out of whatever silhouette is there.

The map stays invisible by default: nothing draws your shape except the grains deciding where to be. Use reveal the map to check it.

The walker field every dot is one grain · stamp a shape or drag to draw · the map is invisible — the crowd is what draws it

0.0020 drift 0.000 ↓ · 0.000 → 0.35
0.28 1.40 0.10 0.55 0.00 0.55 χ is how hard the gradient pulls · α is how much each grain lays down · β decides trails (fast) against blobs (slow) · at 0, grains are points and the crowd can contract without limit
D is the stride² per tick · drift comes from the four walls — feed one and the crowd is carried away from it · k only acts in flow-through

What to try: stamp posts as walls, switch to flow-through, and raise the drift — each post throws a wake and the wakes interleave downstream. Then stamp flui as slow ground in a sealed dish and wait for the letters to fill in. A wall needs drift to show its rim; without a current the crowd settles evenly around it and you get a silhouette-shaped hole instead. Slow ground just needs time. Finally use squint, which bins the grains into a field: that is the view Courses 3–5 work in, built from 24,000 grains rather than 10²³.