Animate a Monte Carlo Simulation in Manim
Animate a Monte Carlo simulation by scattering random points in a square and counting those inside a circle to estimate π as the estimate converges.
To animate a Monte Carlo simulation, scatter random points in a unit square, color the ones that land inside an inscribed circle, and use the ratio to estimate . As points accumulate, the running estimate visibly converges toward 3.14159. It's the perfect first Monte Carlo demo — build it in Manim or generate it from a prompt with QuantumSketch.
The idea in one sentence
A quarter circle of radius 1 has area ; the unit square has area 1. So the fraction of uniformly random points that fall inside the circle approaches , and multiplying by 4 estimates . The animation makes randomness productive — chaos that resolves into a precise constant.
Scatter the points
Drop random dots into the square, coloring each by whether it's inside the circle:
from manim import *
import random
class MonteCarloPi(Scene):
def construct(self):
square = Square(side_length=4).move_to(ORIGIN)
circle = Circle(radius=2).move_to(ORIGIN)
self.play(Create(square), Create(circle))
inside = 0
for i in range(1, 301):
x, y = random.uniform(-2, 2), random.uniform(-2, 2)
in_circle = x*x + y*y <= 4
inside += in_circle
dot = Dot([x, y, 0], radius=0.03,
color=GREEN if in_circle else RED)
self.add(dot)
if i % 20 == 0:
self.wait(0.05)
Green dots are inside the circle, red outside — the picture fills in the geometry as samples grow.
Show the estimate converge
The teaching moment is the running estimate. Display updating live and a reference line at :
from manim import DecimalNumber, ValueTracker, always_redraw
est = always_redraw(lambda: DecimalNumber(
4 * inside / i).to_corner(UR))
self.add(est)
self.wait()
Early on the number jumps around wildly; as samples pile up it settles near 3.14. Watching the variance shrink teaches the law of large numbers without a single equation.
Why this is a great explainer
Monte Carlo feels like magic until you see it. Animation exposes the mechanism: more samples, less noise, better estimate. It generalizes — the same "sample, count, average" pattern estimates integrals, prices options, and powers physics simulations. Showing the simplest case () first gives viewers a hook for all of them.
Generate it from a prompt
Coding the scatter and the live counter by hand is a good exercise; producing a narrated, classroom-ready version is faster with a tool. QuantumSketch builds the storyboard, animation, and voiceover from a prompt like "estimate pi with Monte Carlo and show it converging." The Manim patterns behind it are open in manim-coding-skill.
Frequently Asked Questions
How do you estimate π with Monte Carlo? Scatter random points in a square containing a circle; the fraction inside the circle approaches , so multiply that fraction by 4 to estimate .
Why does the estimate get better with more points? By the law of large numbers, the sample fraction converges to the true probability () as the number of samples grows, so noise shrinks.
What makes Monte Carlo good for animation? The convergence is visual — chaotic early estimates settling toward a constant — which teaches randomness and the law of large numbers at a glance.
Can I make this without writing Manim code? Yes — describe it to QuantumSketch and it generates a narrated animation from the prompt.
Written by Shihab Shahriar Antor — AI Engineer & Founder of Shahriar Labs. Generate simulations like this from a prompt with QuantumSketch; Manim techniques in manim-coding-skill. Also building freelm.