← all posts

Sampling Uniformly Inside a Circle

July 16, 2026  · #monte-carlo #simulation #javascript

Say you want a point placed uniformly at random inside a circle. The naive approach — pick a random angle and a random radius — fails: it clusters points near the center, because it ignores that outer rings have more area than inner ones.

The laziest correct approach is rejection sampling. Draw a point uniformly from the bounding square, and simply throw it away if it lands outside the circle. What’s left is uniform over the disk.

A point (x, y) drawn uniformly from [-1, 1] × [-1, 1] is inside the circle of radius r when:

x² + y² ≤ r²

The two visualizations below share one control panel. Drag the sliders and both respond live — this is a Nanostores atom being read by two separate Astro islands.

// parameters — controls every viz below

batch / tick12
radius (r)1.00
show rejected

The sampler

Green points landed inside the circle and were accepted; red points were rejected. The ratio of accepted to total points approaches the ratio of the areas — circle over square — which is πr² / 4. Rearranging gives a Monte Carlo estimate of π that sharpens as the sample count grows.

// rejection sampler

PAUSED
inside
0
total
0
π estimate
0.0000
inside circlerejected

Notice the estimate is noisy at first and settles as total climbs. That 1/√N convergence is the signature — and the curse — of Monte Carlo methods: to add one decimal digit of accuracy you need roughly 100× more samples.

Is it actually uniform?

A π estimate that converges doesn’t prove the points are evenly spread. For that, accumulate every accepted point into a 40×40 grid and colour each cell by its hit count. If the sampler is unbiased, the interior fills evenly — no clumping, no gaps.

// density heatmap — 40×40 grid

PAUSED
accepted
0
peak cell
0
low densityhigh density

Let it run. The disk fills to a flat, even blue: uniform density, exactly as promised. The only structure you’ll see is the circular boundary itself and the mild jaggedness of a 40-cell grid.

Takeaways

  • Rejection sampling trades a little wasted work (the red points) for correctness that’s trivial to reason about.
  • The acceptance ratio is a free byproduct you can turn into a π estimator.
  • Both widgets auto-pause when scrolled off-screen — check the ACTIVE/PAUSED pill — so a post full of simulations won’t melt your laptop.