Algorithms

The chaos game: random steps draw a Sierpiński triangle

Choose three triangle vertices and a point inside. Repeatedly pick a random vertex and move exactly halfway towards it. A handful of marks looks like a random scatter. Thousands reveal a Sierpiński triangle: a pattern of triangles and ever smaller gaps.

First 12 points: one jump per step. Then up to 100 points per step. Repeated vertex choices are allowed. Click inside the triangle to set a new start; editing restarts playback.

Plotted points: 0. Current point: (50.00, 55.00).

Choose Next to inspect the first midpoint.

Dot colour identifies the chosen vertex: A — orange, B — purple, C — green. The initial point is not included in the count.

Video walkthrough

One rule for every jump

Call the vertices A, B and C. At each turn, choose one, find the midpoint between the current point and that vertex, and plot the midpoint. The next turn starts from the new point. Consecutive choices may repeat a vertex: banning repeats would change the game.

The update is x′ = (x + vx) / 2, y′ = (y + vy) / 2. If the old point is inside the triangle, its midpoint with any vertex also stays inside. This is the invariant of every step. The player uses A=(50,0), B=(0,100), C=(100,100), with y increasing downwards as on a screen. Vertical scaling makes the displayed triangle equilateral.

Inspect a single segment first

Start at P=(50,55). Choosing A=(50,0) gives P′=(50,27.5): x stays unchanged, while y moves halfway. Choosing C=(100,100) next gives (75,63.75). This illustrates two specified choices, rather than predicting the beginning of every random sequence.

Choose 12 jumps · inspect and step forwards. The dashed segment joins the previous point to the chosen vertex; the new point sits at its midpoint. The text below shows that vertex and both coordinate calculations for the last jump. Dot colour identifies the chosen vertex: orange for A, purple for B, green for C. Displayed coordinates are rounded to two decimal places; the model keeps full JavaScript precision.

Why triangular gaps appear

Imagine moving every point of the original triangle halfway towards A. The result is a half-sized copy in the top corner. Choosing B or C produces a matching copy at another corner. Together, these three copies leave an inverted central triangle empty.

Repeat the construction inside each corner copy: again there are three smaller copies and a central gap. After k repetitions, the allowed region consists of 3ᵏ triangles with sides 2ᵏ times shorter than the original. The game applies the same three transformations to a single point and accumulates its positions. This connects midpoint jumps to the self-similar picture.

Early marks can lie in future gaps

The start may be in the central region. Early jumps can then leave marks where the limiting picture looks empty. This is expected. A point in a removed triangle maps into a smaller removed region; subsequent transformations reduce the scale of these regions. The orbit approaches the fractal, and the difference soon becomes invisible at screen resolution.

This player discards no initial jumps and erases no old marks. The starting point has a marker but is not counted as a plotted point. A finite cloud is an approximation to the Sierpiński triangle, not the entire infinite fractal. Pixel size and finite-precision arithmetic also affect what is visible.

Random choices and repeatable runs

In the mathematical game, vertices are chosen independently with equal probabilities. The result about filling the limiting pattern applies to that probabilistic process. Arbitrary sequences need not work: choosing only A makes points approach A rather than reveal the whole pattern.

For repeatable experiments, the player uses a simple 32-bit pseudorandom generator. Its state follows s′ = (1664525·s + 1013904223) mod 2³²; the interval [0,2³²) is divided into three almost equal parts for A, B and C. This is a deterministic sequence with a finite period, not independent ideal die rolls or cryptographic randomness. The same seed and start produce identical points.

From twelve jumps to twelve thousand points

12,000 points · pattern keeps the start and seed but increases the run length. The first 12 steps still add one point each; every later step adds up to 100. At 1×, a player step lasts 0.3 seconds. This shows both the local rule and the emerging pattern without hours of playback.

The counter measures plotted points, not frames or distinct pixels. Coincident points still count as separate jumps. Seeking and stepping backwards restore a saved prefix of the same sequence rather than generate fresh choices. For a batch of 100 points, the formula below the scene describes the final jump.

Executable code for the same sequence

The code below reproduces the player’s vertices, generator, midpoint update and point order. For brevity it assumes validated arguments. It leaves the starting array unchanged; each mark has new coordinates and a vertex index from 0 to 2. The interface validates the seed, point count and starting position.

Show executable code
const vertices = [[50, 0], [0, 100], [100, 100]];
function chaosGame(seed = 42, count = 12000, start = [50, 55]) {
    let random = seed >>> 0;
    let [x, y] = start;
    const dots = [];
    for (let i = 0; i < count; i++) {
        random = (Math.imul(1664525, random) + 1013904223) >>> 0;
        const vertex = Math.floor(random / 4294967296 * 3);
        const [vx, vy] = vertices[vertex];
        x = (x + vx) / 2;
        y = (y + vy) / 2;
        dots.push({ x, y, vertex });
    }
    return dots;
}
// The starting point is not plotted or counted.
// No initial jumps are discarded; a vertex can repeat.

Algorithm memory and player history

A jump takes O(1) time and O(1) working space: only the current point and generator state are required. N jumps take O(N) time. Plotting directly without storing coordinates keeps the algorithm’s memory at O(1), separately from the raster canvas.

This educational player stores N immutable marks and small frame states containing a count: O(N) total memory. Each frame identifies a prefix of the shared history; it does not copy all earlier points. Projecting and redrawing a frame with K points takes O(K) time and temporary memory. The interface limit is 30,000 points, reflecting history and rendering costs rather than a fractal limit.

Three small experiments

Predict a midpoint before choosing Next. Compare 12 and 12,000 points with the same seed: their shared prefix is identical, while accumulated points reveal finer structure. Choose Another sequence: individual marks change, but the recognisable pattern remains.

Click inside the triangle or enter a different start. Keeping the seed preserves the vertex sequence. The separation between two current points halves after every identical choice: after k jumps it equals the original separation divided by 2ᵏ. A shared link includes the start, seed, length, speed and frame; the clean player opens it paused.

Sources

Explore next

Closest pair of points · Voronoi diagrams