Skip to content

Instantly share code, notes, and snippets.

@swflint
Created May 8, 2026 20:51
Show Gist options
  • Select an option

  • Save swflint/f0e97afd10d1cf02f58ad2ae3920b11c to your computer and use it in GitHub Desktop.

Select an option

Save swflint/f0e97afd10d1cf02f58ad2ae3920b11c to your computer and use it in GitHub Desktop.
\documentclass[beamer,tikz]{standalone}
% Define some custom colors
\definecolor{seaborncolorblind1}{RGB}{1,115,178}
\definecolor{seaborncolorblind2}{RGB}{222,143,5}
\definecolor{seaborncolorblind3}{RGB}{2,158,115}
\usetikzlibrary{overlay-beamer-styles}
\usetikzlibrary{calc,math}
\begin{document}
\newcount\ptctr
\begin{standaloneframe}{}{}{}
\begin{tikzpicture}
\draw[step=0.5, gray, very thin] (-1, -1) grid (1, 1);
\draw[black, thick] (-1, -1) rectangle (1, 1);
\draw[black, thick, fill=seaborncolorblind1,nearly transparent] (0, 0) circle [radius=1];
\foreach \x in {-1, -0.5, 0, 0.5, 1} {
\node[anchor=east,text=gray,font=\tiny] at (-1, \x) {\x};
\node[anchor=north,text=gray,font=\tiny] at (\x, -1) {\x};
}
\node [anchor=south, visible on=<1>] at (0, 1.25) {\(\pi \approx 4\frac{|\mathrm{in}|}{|\mathrm{all}|}\)};
\foreach \i [evaluate=\i as \x using rand, evaluate=\i as \y using rand] in {1, 2, ..., 500} {
\pgfmathsetmacro{\d}{sqrt(\x*\x + \y*\y)}
\pgfmathparse{\d <= 1 ? int(1):int(0)}
\ifnum\pgfmathresult>0\relax
\node at (\x, \y) [circle, fill=seaborncolorblind3, seaborncolorblind3, inner sep=0.5pt, visible on=<2>] { };
\global\advance\ptctr by 1\relax
\else
\node at (\x, \y) [circle, fill=seaborncolorblind2, seaborncolorblind2, inner sep=0.5pt, visible on=<2>] { };
\fi
}
\pgfmathsetmacro{\n}{round(int(\the\ptctr) / 2)}
\pgfmathsetmacro{\ourPi}{4 * (\n / 500)}
\node [anchor=south, visible on=<2>] at (0, 1.25) {\(\pi \approx 4\frac{\n}{500} = \pgfmathprintnumber[precision=3]{\ourPi}\)};
\end{tikzpicture}
\end{standaloneframe}
\end{document}
%%% Local Variables:
%%% mode: LaTeX
%%% TeX-master: t
%%% End:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment