Task 1 of 4

Darts at a Quarter Circle

Monte Carlo is statistics as a weapon: throw random darts at a square, and the fraction that lands inside the quarter circle inscribed in it approaches its area — π/4. No geometry beyond the Pythagorean check x² + y² ≤ 1.

The method is embarrassingly parallel: every dart is judged independently, so every dart gets its own thread. One rule, though — the randomness is made outside the kernel. xs and ys hold 4,096 seeded dart positions; the kernel's job is only the verdict. Deterministic data in, deterministic verdicts out — that's what makes GPU Monte Carlo debuggable.

throw darts, count hits — the circle’s area falls out of the ratio
Goal: make each thread return 1 if its dart (xs[x], ys[x]) lands inside the unit quarter circle, else 0.

Requirements

Hint 1 — skip the square root

The dart is inside when its distance to the origin is ≤ 1 — and distances compare the same way squared: x * x + y * y <= 1 is the whole test.

Hint 2 — the verdict
if (x * x + y * y <= 1) {
  return 1;
}
return 0;

— a branch is fine in a kernel as long as every path returns.

Same idea elsewhere

Real GPU Monte Carlo keeps the random numbers on-device — CUDA ships cuRAND, and WebGPU/Metal compute shaders run counter-based generators like Philox per thread — but the shape is exactly this: one thread, one sample, one verdict.

All tasks in Monte Carlo Methods

  1. Darts at a Quarter Circle
  2. Reduce 65,536 Hits to π
  3. Integrate the Un-integrable
  4. Price an Option

This page is an interactive exercise — the editor, the GPU runner and your saved progress need JavaScript. The text above is the full brief.