A fern from four coin flips
The Barnsley fern, published by mathematician Michael Barnsley in 1988, is a demonstration that an intricate, natural-looking shape can be encoded in a handful of numbers. It is an iterated function system (IFS): a set of affine transformations -- each one a combination of scaling, rotation and translation -- applied over and over to a single point, with one transformation chosen at random on each step according to a fixed probability.
each transform: x' = a*x + b*y + e
y' = c*x + d*y + f
f1 (1% chance) -- stem: maps everything to a thin vertical line
f2 (85% chance) -- successive leaflets: main self-similar copy, slightly shrunk & rotated
f3 (7% chance) -- largest left leaflet
f4 (7% chance) -- largest right leaflet
The Chaos Game
Running an IFS this way is called the Chaos Game: start anywhere (the origin works fine), then repeat forever -- pick one of the four transforms at random using its assigned probability, apply it to the current point to get a new point, plot it, and repeat from the new point. Individually each step looks like noise; the point jumps unpredictably around the plane. But after a few thousand iterations, a shockingly precise fern emerges from the fog. This is the defining trick of the Chaos Game: randomness in the choice of rule, determinism in the geometry it converges to. The attractor of the IFS -- the fixed shape the orbit is drawn onto -- does not depend on the starting point or on the exact sequence of random choices, only on the four transforms and their relative scales.
Why the probabilities are weighted, not equal
The 1/85/7/7 split looks arbitrary but is chosen deliberately to match the area each transform maps onto relative to the whole fern. f2 (the self-similar leaflet-generating map) covers by far the largest fraction of the fern's area, so it needs to be selected far more often than f1, which only ever draws the thin stem, otherwise the stem would be drawn thousands of times more densely than it should be relative to the leaflets and the rendered density would misrepresent the shape. Formally, weighting each transform's probability by (roughly) the determinant of its linear part -- its area-scaling factor -- is what makes the point density converge to the fern's natural invariant measure instead of an arbitrarily biased one.
Self-similarity: why it looks like a fern at all
Transform f2 is the reason the shape resembles a real fern frond: applying it maps the whole fern onto one of its own leaflets, slightly smaller and rotated. Since that leaflet is itself a scaled copy of the whole, and it in turn is built from its own smaller leaflet-copies, the structure repeats at every scale -- the defining property of a fractal. Real ferns display an echo of this because their growth is also governed by a repeated, self-similar branching rule at the level of a growing meristem, though biological growth adds constraints (light, nutrients, gravity) that an idealised IFS ignores; the mathematical fern is a caricature of the growth process, not a literal model of it.
Rendering: why millions of points and not one big pass
Each iteration only costs one matrix-vector multiply and one random draw, so a modern browser can plot hundreds of thousands of points per second. Detail improves purely by running longer -- there is no separate refinement pass, no mesh, no recursion depth to worry about, because the entire fractal is defined implicitly by the long-run statistics of the random orbit rather than by an explicit recursive construction. This is also why an IFS render is a genuinely different algorithmic species from the escape-time colouring used for the Mandelbrot set: there, every pixel is tested independently against a fixed iteration; here, the same single trajectory is what paints every pixel, one at a time, and the image only converges as the trajectory's history accumulates.
Frequently asked questions
Why does the fern always come out the same shape no matter where the point starts?
The four affine transforms are contractions -- each one shrinks distances -- so however far apart two starting points are, applying the same random sequence of transforms to both makes them converge toward each other. The long-run set of visited points (the attractor) is therefore determined only by the transforms and their probabilities, not by the initial point.
Why are the probabilities 1%, 85%, 7%, 7% instead of equal 25% each?
The probabilities are set roughly proportional to how much area each transform's image covers within the whole fern. The stem-drawing transform covers a tiny sliver of area and so is picked rarely (1%); the main leaflet-generating transform covers most of the fern's area and is picked 85% of the time, which keeps the plotted point density visually proportional to the fern's real structure.
Is the Barnsley fern a model of how real ferns grow?
Only loosely -- it captures the idea that a fern frond is built from smaller self-similar copies of itself, which is a real feature of plant branching, but it ignores every biological constraint such as light response, nutrient availability and gravitropism that shape an actual growing fern. It is best understood as a minimal mathematical illustration of self-similarity, not a botanical growth model.
Try it live
Everything above runs in your browser — open Barnsley Fern and change the parameters while it is running. Nothing is installed, nothing is uploaded, the whole model lives in one tab.
▶ Open Barnsley Fern simulation