This post is inspired after watching StandUpMaths video "Generating π from 1,000 random numbers".

He showed how to do this in Windows Excel after doing it manually. And it wasn't hard to program this because he went through the steps, but I still think it's cool.

Here's the code I used:

``````--// Public Vars
local numbers = 10000000
local numSize = 1000000

--// Local Functions
local function gcd(a,b)
if(b~=0)then
return gcd(b, a%b)
else
return math.abs(a)
end
end

--\\ Code:
local count = 0
for i = 1,numbers do
count = count + (gcd(math.random(numSize), math.random(numSize))==1 and 1 or 0)
end
local pi = math.sqrt(6/(count/numbers))
print(pi)
``````

And I just followed the steps from the video. I got the GCD function online here.

If you run this code yourself you might want to use a lower "numbers" number for the variable. This took about 1-2 minutes for studio to start responding again after hitting play.

It was pretty accurate though. My output was `3.1415414992732`, which is within `0.001628319%` to pi, which is really close!