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.

By Shihab
3 min read

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 π4insidetotal\pi \approx 4 \cdot \frac{\text{inside}}{\text{total}}. 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 π/4\pi/4; the unit square has area 1. So the fraction of uniformly random points that fall inside the circle approaches π/4\pi/4, and multiplying by 4 estimates π\pi. 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 4insidetotal4 \cdot \frac{\text{inside}}{\text{total}} updating live and a reference line at π\pi:

        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 (π\pi) 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 π/4\pi/4, so multiply that fraction by 4 to estimate π\pi.

Why does the estimate get better with more points? By the law of large numbers, the sample fraction converges to the true probability (π/4\pi/4) 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.

Tags:#manim#monte-carlo#simulation#math-animation#statistics
S

Shihab Shahriar

AI Engineer & Founder of Shahriar Labs. Exploring the intersection of design, cognition, and machine learning.