Task 1 of 4
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.
1 if its dart
(xs[x], ys[x]) lands inside the unit quarter circle, else 0.xs[this.thread.x] and ys[this.thread.x]x² + y² ≤ 1 — no Math.sqrt needed1 or 0, nothing in betweenThe 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.
if (x * x + y * y <= 1) {
return 1;
}
return 0;
— a branch is fine in a kernel as long as every path returns.
This page is an interactive exercise — the editor, the GPU runner and your saved progress need JavaScript. The text above is the full brief.