# Approximating pi utilizing… a cake?

*by*Phil Tadros

**Tuesday, March 14, 2023**

Completely happy Pi Day, fellow nerds!

It is a vacation I’ve celebrated yearly since at the very least 2010, and I am not stopping anytime quickly.

The celebrations have developed.

It was simply “bake a pie” and “haha pi, pie”.

Over time, I twisted it a bit (pizza is a pie of types! a cake with a pi image on it!).

This yr is the following evolution.

I’ve made a cake with an experiment on it for estimating the worth of pi.

It is a actually cool method referred to as Buffon’s needle problem and I first heard about it from my grandfather at a restaurant.

I believe I used to be in center faculty.

Anyway, he was telling me about this fashion that you can estimate pi by tossing a needle on the ground and counting the variety of instances the place it ended up crossing the road between ground boards.

I did not actually *get* it then, however it caught in my thoughts that it was actually neat that you can do that factor to estimate the worth of pi!

I understood it had one thing to do with the needle having the ability to kind a circle (rotated round its middle) and a few such.

Quick ahead to 2023, and I am sitting idly desirous about Pi Day plans, and I understand.

I could make a cake.

I can draw traces on it.

I’ve sprinkles.

We are able to do Grandpa Invoice’s pi needle estimate, however on a *cake*!

First, I’ve to determine what’s that even that he had informed me about.

It was straightforward sufficient to seek out the Wikipedia web page for Buffon’s needle problem.

The unique formulation wasn’t round estimating the worth of pi, however it positive can be utilized that method.

Mainly, you will have this method: `p = (2/pi) * (l/t)`

, the place:

`p`

is the likelihood that the needle will cross the road between two ground boards`l`

is the size of the needle`t`

is the width of the ground boards

We are able to reformulate this as `pi = (2/p) * (l/t)`

, after which can derive an estimate of pi from an estimate of the likelihood that the needle crosses a ground board.

Or the likelihood {that a} sprinkle crosses a line on a cake.

You see the place that is going.

We’ll “bake” a cake on an HTML canvas, and do a Monte Carlo simulation of the worth of pi.

The very first thing we have to do is setup our canvas.

We make the factor, and set some types in order that it is sq. and as large as may be, however not *too* large, we’re not monsters.

```
<canvas id="needles" model="aspect-ratio: 1/1; show: inline-block; width: 100%; max-width: 400px;"></canvas>
```

Then we perform a little little bit of JS to make the canvas scale to the dimensions of the factor.

We add the traces on the cake, and we add sprinkles on it.

The code is all obtainable in the repo, so I will not go into element on all of it right here.

However there’s this one actually cool bit I ran throughout whereas coding it up.

How do you place the sprinkle going through a random route?

My first thought was to generate a random angle after which compute the sprinkle vector from there.

That both depends on choosing an angle in radians (thus counting on pi) or utilizing sine or cosine, which additionally feels prefer it’s in opposition to the spirit of estimating pi.

So what to do?

Enter: the unit circle!

I discovered a cool blog post which talked about an algorithm from von Neumann himself.

The important thing perception is that you probably have a uniformly distributed random quantity in a variety, you possibly can map that onto the unit circle (and hold regenerating in case you are outdoors the unit circle).

Then you possibly can scale it to land on the circle, as an alternative of inside it, and also you now have a random level on the circumference of the unit circle!

Let’s have a look at that in code.

```
// Generate a vector at a random angle between -90 and 90 levels
operate randomAngleUnitVector() {
// If we're not contained in the unit circle, we'll hold retrying
// till we succeed. This could cross fairly rapidly.
whereas (true) {
// Math.random() offers us a uniform distribution in [0,1].
let x = Math.random();
let y = 2 * Math.random() - 1;
let r = Math.sqrt(x*x + y*y);
if (r <= 1) {
// We bought it, so we'll scale the vector out to the circle
return [x / r, y / r];
}
}
}
```

I sprinkled (pun supposed) some feedback in.

The core thought right here is so cool and intelligent.

Glad it is in my instrument bag now.

So now we now have every part we’d like.

The cake’s been within the oven and, oh look, **it is completed**.

Let’s pull it out and see what we bought!

I left some sliders down under so that you can mess around with.

You may drag them round to play with totally different parameters, like the dimensions and amount of sprinkles, and see how that impacts the estimate of pi.

Simply do not forget that since this can be a simulation, you will get very totally different values every time.

So if you wish to see if parameters improved it, it’s possible you’ll need to click on “bake” just a few instances to see a clearer image of the change.

This cake estimates pi as .

The working estimate for these params is .

You have baked of this type.

Oh yeah, and I did this in real-life, too.

This is the pi-approximation cake in all its glory.

If this publish was pleasant or helpful for you, **please share it!**

When you’ve got feedback, questions, or suggestions, please e mail my public inbox or my personal email.

To get new posts, please use my RSS feed.