Pero Grubač

Monte Carlo π · Python

Estimates π with NumPy: in the browser, a Jupyter notebook and a CLI.

Monte Carlo π · Python: random points filling a circle inside a square, next to the current estimate, the point counts, the run settings and the convergence chart

What it does

A circle of radius 1 inside the square [−1, 1]² covers π/4 of it, so four times the share of random points that land inside the circle approaches π. The montecarlo_pi package does this with NumPy and says how precise the result is: the standard error, a 95% interval, the correct decimals, and how many points real certainty would take. The same package runs in three places:

  • In the browser, the square fills live, the correct digits turn green and a chart follows the convergence. It is real Python and NumPy, running locally through Pyodide; the first visit downloads about 10 MB.
  • A Jupyter notebook walks through the idea: one run, convergence, the points needed per decimal, and a thousand runs against the normal curve.
  • The command line prints one estimate with its error, interval and speed.

A run throws a set number of points or goes until the decimals match, and a seed repeats it exactly in all three. Every extra correct decimal costs 100 times more points: being 95% sure of four decimals takes about 4.1 billion points, a minute and a half at NumPy speed. The decimal search usually stops far sooner, because a random estimate wanders through the right window by chance: with seed 19, four decimals match after 1,477 points, which is luck, not precision.

How it works

Points are drawn as NumPy arrays, in blocks of about a million, which gives about 45 million points per second on one core and about 28 million in the browser.

“Until decimals match” stops at exactly the first point whose estimate falls in the target window, such as [3.1415, 3.1416) for four decimals. Before each block, the simulation saves the state of NumPy’s PCG64 random stream. A cumulative sum of the inside counts then gives the estimate after every single point of the block. If one lands in the window, the simulation restores the saved state and advances the stream past exactly the points it used, so the stream continues right after that point, as if the points had been drawn one by one.

In the browser, JavaScript loads Pyodide, runs the loop and draws; each step calls a small Python bridge into the same Simulation class that the notebook and the command line use.

Decisions and why

  • One package, three front ends. The page does not reimplement the maths in JavaScript. It loads the same Python files into Pyodide, so a seed gives the same answer everywhere.
  • Blocks, not loops. The first version drew one point at a time with np.random.uniform and appended tuples to a list. A million points as one array is close to 200 times faster (2 ms against 0.40 s per 100,000 points) and keeps memory flat.
  • Exact stops, still reproducible. Because the stream is advanced past exactly the points used, a later run continues as if the block had not overshot.
  • A static page. No server and no build step. A Streamlit app would need a server that sleeps when idle.
  • Small batches first. The page starts at 25 points per frame, so the first points can be watched as they land, then sizes its batches from the measured speed to keep the page responsive.

Testing and deployment

32 pytest tests and ruff run in GitHub Actions on Python 3.11 and 3.13, and CI also executes the notebook, so it never breaks. A second workflow publishes the page to GitHub Pages when the page or the package changes on master.

HomepageAboutsectionSystemssectionIndex of projectssectionBrowser appspageContactsectionIncident Alertsystem · case studyeMobilityHubsystem · case studyChat Roomsystem · case studyOnline Library Systemsystem · case studyRepoHealthlive app · case studyOpen RepoHealth app (opens in a new tab)live appGitHub Discord Bottool · case studyBookstore APIweb project · case studyFitForgemobile app · case studyAssetManagermobile app · case studyStore Managementdesktop app · case studyETF Oglasimobile app · case studyDevKitlive app · case studyOpen DevKit app (opens in a new tab)live appDev Timelinelive app · case studyOpen Dev Timeline app (opens in a new tab)live appGitHub Dashboardlive app · case studyOpen GitHub Dashboard app (opens in a new tab)live appStore Chain Managementdesktop app · case studySecure Repositorytool · case studyMinimal Blogweb project · case studyCatan Map Generatorlive app · case studyOpen Catan Map Generator app (opens in a new tab)live appMonte Carlo π · Pythonlive app · case studyOpen Monte Carlo π · Python app (opens in a new tab)live appBrick Breakerlive app · case studyOpen Brick Breaker app (opens in a new tab)live appArcane Keeplive app · case studyOpen Arcane Keep app (opens in a new tab)live appe-Mobility Rentaldesktop app · case studyCountdownlive app · case studyOpen Countdown app (opens in a new tab)live appANOVAdesktop app · case studyEcho Playerdesktop app · case studyMonte Carlo πdesktop app · case studyexcuse.exelive app · case studyOpen excuse.exe app (opens in a new tab)live appYamb Tablelive app · case studyOpen Yamb Table app (opens in a new tab)live appSedmicalive app · case studyOpen Sedmica app (opens in a new tab)live appLora Score Tablelive app · case studyOpen Lora Score Table app (opens in a new tab)live appSnake (Blavor)desktop app · case study
Copy emailpero.grubac00@gmail.com