Skip to content
Tactiverge

✦ Simulation · randomness and reproducibility

Monte Carlo with a seeded RNG

Estimate π by throwing random points at a square, using a seeded generator so results are reproducible.

  • Level 5 · Research Engineer
  • 5 tests
  • technically checked

Your task

Monte Carlo methods answer hard questions by sampling. Throw random points into the unit square; the fraction landing inside the quarter circle x² + y² ≤ 1 approaches π/4.

Engineering simulations must be reproducible, so use the provided seeded generator: const rng = makeRng(seed) returns a function, and each rng() call gives the next number in [0, 1). The same seed always gives the same sequence.

Write estimatePi(samples, seed): for each sample take x = rng() then y = rng(), count the points inside, and return 4 × inside / samples.

Hints (0 of 3 shown)

    What gets checked

    • estimatePi(10, 42) → 3.6
    • estimatePi(1000, 7) → 3.232

    Plus 3 hidden tests for edge cases. They run in the sandbox like the others; only the result is shown.

    Tab indents · Esc then Tab leaves the editor · Ctrl/⌘ Enter runs · Ctrl/⌘ Shift Enter checks · Ctrl/⌘ / comments

    Starting the sandbox…

    Press Run to see what your program prints.

    Your code runs only in this browser, inside an isolated sandbox with no network access, and stops after 3 s. Nothing you type is sent to Tactiverge; drafts and progress are saved in this browser only.