# Monte Carlo methods

We'll consider the upper-right quarter of the unitary disk centered at the origin. Its area is π 4 {\displaystyle \pi \over 4} ![{\displaystyle \pi \over 4}](https://wikimedia.org/api/rest_v1/media/math/render/svg/a6a0b09884a506293568263efe49ac399804925b).

```perl
my @random_distances = ([+] rand**2 xx 2) xx *;

sub approximate_pi(Int $n) {
    4 * @random_distances[^$n].grep(* < 1) / $n
}

say "Monte-Carlo π approximation:";
say "$_ iterations:  ", approximate_pi $_
    for 100, 1_000, 10_000;
```

#### Output:

```
Monte-Carlo π approximation:
100 iterations:  2.88
1000 iterations:  3.096
10000 iterations:  3.1168
```

We don't really need to write a function, though. A lazy list would do:

```perl
my @pi = ([\+] 4 * (1 > [+] rand**2 xx 2) xx *) Z/ 1 .. *;
say @pi[10, 1000, 10_000];
```
