Snapshot / Computational Thinking Repo ↗

Random walks in two dimensions

A one-dimensional walker could only go left or right. Set it free on a plane and at each step it picks one of four directions — up, down, left, right — at random. The path it traces is the classic picture of diffusion: a wandering, self-crossing thread that slowly spreads outward from the start.

This is exactly how a pollen grain jitters in water (Brownian motion), how a gas molecule diffuses, and how heat spreads. Drag the sliders to send the walker on a longer journey or a different random path, traced live in your browser as WebAssembly.

begin
    using PlutoUI, WasmMakie
end

steps = 1500

seed = 11

let
    # walk the plane: each step draws a direction 0..3 from one inline RNG stream and
    # moves one unit that way. We record the whole (x, y) trail to draw it.
    s = (seed * 2654435761 + 12345) % 2147483647
    if s == 0
        s = 1
    end
    xs = Vector{Float64}(undef, nsteps + 1)
    ys = Vector{Float64}(undef, nsteps + 1)
    x = 0.0
    y = 0.0
    xs[1] = x
    ys[1] = y
    for i in 1:nsteps
        s = (s * 16807) % 2147483647
        d = s % 4                       # a direction: 0,1,2,3
        if d == 0
            x = x + 1.0
        end
        if d == 1
            x = x - 1.0
        end
        if d == 2
            y = y + 1.0
        end
        if d == 3
            y = y - 1.0
        end
        xs[i + 1] = x
        ys[i + 1] = y
    end
    fig = Figure(size = (480, 480))
    ax = Axis(fig[1, 1])
    lines!(ax, [0.0], [0.0])     # mark the origin
    lines!(ax, xs, ys)           # the wandering trail
    fig
end
rw2_stats = let
    # compute the readout numbers in a SEPARATE bond-dependent cell (returns a tuple)
    # so the markdown below can interpolate it live — values buried inside a markdown
    # cell's own `let` get baked to the slider defaults.
    s = (seed * 2654435761 + 12345) % 2147483647
    if s == 0
        s = 1
    end
    x = 0.0
    y = 0.0
    for i in 1:nsteps
        s = (s * 16807) % 2147483647
        d = s % 4
        if d == 0
            x = x + 1.0
        end
        if d == 1
            x = x - 1.0
        end
        if d == 2
            y = y + 1.0
        end
        if d == 3
            y = y - 1.0
        end
    end
    (floor(sqrt(x * x + y * y) * 100.0) / 100.0, floor(sqrt(Float64(nsteps)) * 100.0) / 100.0)
end;

After 1500 steps the walker is 63.78 units from where it started, while the typical distance grows like sqrt(steps) = 38.72. Diffusion is slow: to wander twice as far from home you need four times as many steps.

From one walker to a physical law

Watch many such walkers at once and they spread out as a widening, blurry cloud — and the density of that cloud obeys the diffusion equation, the same partial differential equation that governs heat flow and the smearing of a drop of dye. A simple random rule for one particle, averaged over many, becomes a smooth deterministic law for the whole. That bridge from random microscopic steps to predictable macroscopic behaviour is one of the great themes of computational science.

Appendix

Built with an inline Park-Miller random generator and WasmMakie so the whole walk runs as in-browser WebAssembly — no rand, no Plots.jl, no server. The four-direction rule and the sqrt(steps) spread are the standard textbook 2D random walk.