Galton Lines — Chatlog
Tree-diagram model • Coin flips at horizontal lines • No physics • Pure probability
Companion chatlog for Galton Peg Simulator
This log documents the creation of galtonLinesSimAppV1.html, a new model for demonstrating the Central Limit Theorem. Where the Peg Simulator uses physical collision mechanics (and fought a long battle with peg geometry, double-collision bias, and ball-sticking), the Lines model eliminates all physical simulation in favour of a pure probability tree diagram.
A ball makes exactly n independent binary decisions — one at each horizontal line — and travels in a straight-line segment between decisions. No gravity, no bounce, no radius arithmetic. The result is a cleaner model and a visually compelling tree-path display when trails are enabled.
You are a web developer, UX specialist and applied mathematician. You help develop apps at TNT. If you study the files I’ve attached you’ll see we are experimenting with various iterations on a ‘Galton Board’ simulation. There are victories but problematic modelling issues especially with smaller numbers of rows and the co-dependency of spacing, peg size, ball size and row spacing. I had another thought: It would be a change in model with the same basic idea, but more ‘precise’ since it’s the collision physics that’s causing some issues. Here’s the scenario: instead of a row of pegs, we have a single faint line across the board. Whereever a ball hits a line, a coin is flipped that determines whether it veers to the left or right by ‘deltaDeg’ degrees to the east or west of north. After the last line, we can collect the balls like we did in the original bar compartments. Let’s try this rendition with galtonLinesSimAppV1.html with associated CSS, sketch and JavaScript support files. The control panel will be simplified: we just need sliders for the vertical spacing between lines and a way of adjusting the deltaDeg value. I want to have a separate chatlog for this endeavor: galtonLinesChatlog.html with its associated CSS and JS files. When we get this working, we can find a way to reference the best of all our apps into a framework that allows experimentation with various aspects of the problem. I think this approach will be more precise. We could also have an option to show the ‘trails’ of the balls moving between the lines.
[Follow-up:] I don’t see that we need gravity to start with. It’s more of a tree diagram, not a physical model.
A strong architectural prompt that correctly diagnoses the root cause of the peg model’s instability (co-dependency of four physical parameters) and proposes a mathematically cleaner alternative. The follow-up clarification (“no gravity, tree diagram not physical model”) is excellent prompt discipline: it arrives immediately and resolves the most consequential ambiguity before any code is written.
✅ Strengths
⚠️ Growth Areas
Grade rationale: A−. The architecture is sound, the root cause is correctly identified, the deliverables are enumerated, and the follow-up clarification arrived before any code was written. The cumulative deflection description and the missing bin-assignment rule are the only gaps.
Each ball maintains an integer deflection counter, initially 0. At each line crossing:
deflection += (Math.random() < 0.5) ? -1 : +1;
The ball’s travel angle is then deflection × deltaDeg degrees from vertical. Direction components:
var rad = radians(deflection * deltaDeg); vx = sin(rad) * speed; // horizontal component vy = cos(rad) * speed; // vertical component — always > 0 for |angle| < 90°
Because vy = cos(angle) × speed and cos(0°) = 1, a ball at zero deflection moves straight down. Because cos(angle) decreases as angle increases, steep-angle balls move slower vertically — a natural consequence of the vector decomposition, not a bug. For |deflection × deltaDeg| ≥ 90°, the ball would stop descending; the slider max (20°) and line count max (16) give a maximum angle of 320°, which would be pathological. In practice, the CLT ensures that extreme deflections are vanishingly rare, but a future version should add a guard.
When a ball’s y position reaches GL_BOARD_BOT, its bin is assigned by:
bin = constrain(floor((x - BOARD_LEFT) / binWidth), 0, numBins - 1);
This means the distribution is determined by the sum of x-displacements across all segments, not by the final deflection count. The distribution of this sum converges to Normal by the CLT but is not exactly binomial. It has variance proportional to n³ rather than n, and produces a wider spread than the peg model for the same number of lines.
Alternative: assign bins by final deflection count directly. This would produce the exact binomial distribution but would require the bins to be centered differently (deflection can range from −n to +n). The x-position approach was chosen because it makes the visual (ball path → bin) self-explanatory.
The gold curve displays the binomial PMF, as in the peg simulator. This is exact for the peg model (where ball trajectories are constrained to land in discrete positions), but only approximate for the lines model (where x-position is continuous and the distribution is the random-walk-sum).
The approximation improves as Δ° → 0, because for small angles:
sin(d × deltaDeg) ≈ d × deltaDeg_rad x_final ≈ LINE_SPACING × deltaDeg_rad × ∑(deflection_k for k = 1..n)
The sum of a random walk’s cumulative values has a distribution that is asymptotically normal but with larger variance than binomial. For practical deltaDeg values (1°–10°) the gold curve gives a reasonable visual guide. A future version could compute the true distribution numerically via simulation rather than analytically.
With trails enabled, each ball records its x,y position at every step and draws a polyline behind itself. The result, over many balls, is a direct visual of Pascal’s Triangle: each line crossing is a branch point, and the branch weights are proportional to the binomial probabilities.
Trail rendering uses HSB color based on the current deflection: blue for left-deflected balls, red for right-deflected, teal/white for centered. At low drop rates (one ball at a time) with trails on, the branching tree structure appears in the background. This is the visual payoff that the peg model could never produce cleanly — the peg physics randomize the path in ways that obscure the tree structure.
The peg simulator uses a navy/steel-blue palette. The lines simulator uses dark teal (--gl-teal: #00c8b2) to make the two simulators immediately visually distinct when used side by side in a comparison framework. Both retain the gold theoretical-curve color so the comparison point is clear even across the different palettes.
galtonLinesSimAppV1.html — Main simulator page. Teal hero, simplified two-column layout. Controls: Drop 1/50/200/Auto, Reset, Number of Lines (1–16), Δ° (1–20), Ball Speed, Drop Interval, Trails toggle, Reset Configuration. Statistics panel: Total, Mean, Median, Mode, Std Dev, Shape. Gold-curve note explaining it is an approximation.
styles/galtonLinesSimAppStylesV1.css — Dark teal palette. --gl-* custom properties. Same structural component classes as peg simulator (ctrl-card, slider-group, btn-gl-drop, stat-row) for component consistency.
sketches/galtonLinesSimAppSketchV1.js — Pure p5.js instance-mode sketch. No gravity. Balls travel as straight-line segments. stepBall() adds trail points, updates x/y, checks line crossings with a while loop (handles multi-line skips at high speed). Gold binomial PMF curve uses curveVertex() for n>2, vertex() for n≤2. Public API: glDropBall, glDropMany, glToggleContinuous, glReset, glSet*, glGetStats.
scripts/galtonLinesSimAppScriptsV1.js — Slider/button event handlers, 500ms stats polling, auto-drop button sync.
galtonLinesChatlog.html — This file.
styles/galtonLinesChatlogStylesV1.css — Teal chatlog palette. Same structural patterns as peg chatlog CSS but differentiated with --gl-* variables.
In the peg model, a ball’s final bin is determined by the count of rightward deflections — a binomial(n, 0.5) random variable. In the lines model, the final x position depends on the sum of x-displacements across all segments:
x_final = ∑_{k=1}^{n} LINE_SPACING × sin(deflection_k × deltaDeg_rad)
≈ LINE_SPACING × deltaDeg_rad × ∑_{k=1}^{n} deflection_k (for small deltaDeg)
where deflection_k is the random walk value at step k. The quantity T_n = ∑ deflection_k is the area under a random walk — a well-studied object in probability theory. Its variance is:
Var(T_n) = n(n+1)(2n+1)/6 ≈ n³/3 for large n
Compared to the binomial variance n/4, the lines model produces a spread that grows like n3/2 rather than n1/2. For large deltaDeg this makes a visually wider distribution. The distribution is still normal (by CLT), so the bell-curve shape is preserved, but the gold binomial overlay will be narrower than the actual empirical distribution. Future versions can display the empirically-fitted normal curve instead.
With trails enabled and one ball dropped at a time, the accumulated trails form a visible tree diagram. Each line crossing is a binary branch point. The branch angles are ±deltaDeg from the current direction. After n lines there are at most 2n distinct paths, and the number of paths arriving at each bin follows the binomial coefficients C(n,k).
This is the most direct visual link between the simulation and Pascal’s Triangle. Students can watch individual ball paths, count branches, and see that the most-used paths are the ones with alternating left/right choices — the paths that stay near center, which also correspond to the largest binomial coefficient C(n, n/2).
The initial sketch used deflection += flip (additive/cumulative). This makes L→R cancel to 0° — a left then right sends the ball straight down. That is not what the user described and does not mimic the peg model.
| Model | Code | L then R gives… | Distribution |
|---|---|---|---|
| A — Cumulative (buggy) | deflection += flip |
0° (straight down) | Random-walk sum, variance ∝ n³ |
| B — Reset (correct) | deflection = flip |
−15° then +15° | Exact binomial(n, 0.5) ✓ |
Why Model B is binomial: Every segment moves the ball horizontally by the same magnitude — ±LINE_SPACING × tan(deltaDeg). The sign depends only on the coin flip. After n lines:
x_final = center + (rights - lefts) × LINE_SPACING × tan(deltaDeg)
= center + (2 × rights − n) × LINE_SPACING × tan(deltaDeg)
→ “rights” follows Binomial(n, 0.5) → distribution IS binomial ✓
This is exactly how the peg model works: each peg deflects the ball by a fixed lateral magnitude regardless of incoming angle. The gold binomial PMF overlay will now match the empirical distribution closely.