Estimating π, one dart at a time
A parameterized Monte Carlo estimate of π: tune the sample count in the parameters form or from the CLI, simulate in C#, and let Python draw the dartboard.
Estimating π, one dart at a time
Throw darts at a square; count how many land inside the quarter circle. The ratio
approaches π/4. The parameters cell below makes the experiment tunable without
touching code: run it headlessly with a million samples via
verso run monte-carlo.verso --param samples=1000000, or slide the values in the
editor's form. C# runs the simulation; Python draws the dartboard.
verso run --param name=value. Shown here: the defaults.1// parameters arrive as 64-bit integers; Random wants an int2var rng = new Random((int)seed);34var inside = 0;5var plotX = new List<double>();6var plotY = new List<double>();7var plotHit = new List<int>();89for (var i = 0; i < samples; i++)10{11 var x = rng.NextDouble();12 var y = rng.NextDouble();13 var hit = x * x + y * y <= 1.0;14 if (hit) inside++;1516 if (i < 2500) // keep the scatter plot light17 {18 plotX.Add(x);19 plotY.Add(y);20 plotHit.Add(hit ? 1 : 0);21 }22}2324var piEstimate = 4.0 * inside / samples;25$"π ≈ {piEstimate:F5} after {samples:N0} darts (true value 3.14159...)"
π ≈ 3.14900 after 20,000 darts (true value 3.14159...)
1import math2import matplotlib.pyplot as plt34xs = [float(x) for x in plotX]5ys = [float(y) for y in plotY]6hits = [int(h) for h in plotHit]78fig, ax = plt.subplots(figsize=(5.4, 5.4))9ax.scatter([x for x, h in zip(xs, hits) if h], [y for y, h in zip(ys, hits) if h],10 s=6, color="#0078d4", label="inside")11ax.scatter([x for x, h in zip(xs, hits) if not h], [y for y, h in zip(ys, hits) if not h],12 s=6, color="#dc3545", label="outside")13arc = [t / 200 * math.pi / 2 for t in range(201)]14ax.plot([math.cos(t) for t in arc], [math.sin(t) for t in arc], color="#161510", linewidth=1.2)15ax.set_aspect("equal")16ax.set_title(f"π ≈ {piEstimate:.5f}")17ax.legend(frameon=False, loc="upper right")18plt.tight_layout()19plt.show()
Why this matters beyond π
Swap the dart experiment for anything you can sample: pricing scenarios, latency budgets, A/B outcomes. Parameters make the notebook a reusable tool instead of a one-off script, and the CLI turns it into a batch job: same file, no edits.
Run it yourself
Nothing on this page is executable; the outputs were baked in by a real run. Download the notebook and it runs anywhere Verso does: VS Code, the browser, or the terminal.
dotnet tool install --global Verso.Cliverso serve monte-carlo.versoWhat this shows
A parameterized Monte Carlo estimate of π: tune the sample count in the parameters form or from the CLI, simulate in C#, and let Python draw the dartboard.
Every language in this notebook reads and writes the same variable store, so a value produced in one cell is simply there in the next, whatever the language.