add tag
samcarter
> This is part of the Summer of Code 2022 series, see https://topanswers.xyz/tex?q=2059 for more information

Today's puzzle is a fun one: calculate pi


![SoC.png](/image?hash=4c8fc86bb91a1863146b746f22d11e11f5927d1834c13f48a48251c71a81d213)
Top Answer
samcarter
*no spoiler*

Today, I wanted to learn about random numbers in expl3, so I decided to go for a Monte Carlo approach to approximate pi:

```
\documentclass{article}

\begin{document}

Today’s puzzle is a fun one: calculate pi

\ExplSyntaxOn

\int_new:N \l_sam_a_int
\int_new:N \l_sam_b_int
\int_new:N \l_sam_count_int
\int_zero:N \l_sam_count_int
\int_new:N \l_sam_rep_int
\int_set:Nn \l_sam_rep_int { 1000000 }

\int_step_inline:nn {\l_sam_rep_int}
{

  \int_set:Nn \l_sam_a_int { \int_rand:n {10000} }
  \int_set:Nn \l_sam_b_int { \int_rand:n {10000} }
  
  \int_compare:nNnT { \l_sam_a_int * \l_sam_a_int + \l_sam_b_int * \l_sam_b_int } < { 10000 * 10000 } { \int_incr:N \l_sam_count_int } 
  
}

\par \fp_eval:n { \l_sam_count_int * 4.0 / \l_sam_rep_int }

\ExplSyntaxOff

\end{document}
```

This room is for discussion about this question.

Once logged in you can direct comments to any contributor here.

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.