I got inspired by a recent youtube video I watched called Generating π from 1,000 random numbers.
In this video the presenter Matt Paker shows you how to calculate PI from 1000 random numbers.
I decided to code this in JS. This is slightly different as I decided to get the average of 1000 attempts at calculating pi from 1000 random numbers.
The 3 constants upper, sampleSize and attempts can be used to reconfigure this.
- Upper is the upper bound of the random number.
- sampleSize is the number of random number pairs to use.
- attempts is the number of times we calculate pi.
There are a few caveats to using this
- I have used Arrays so there is a maxiumn number of both sampleSize and attempts 4.29 billion.
- It takes it a bit to work out all of this so it may pause before giving an answer the pause length is really how fast your machine is.
To save me a load of time I used mathjs so you will need to install that.
then simple run the script with node
node ./CalculatePi.js
Dont want to go to all the fuss of installing node then using RunJS works , It was what I did this in myself. you can read how to install mathjs at https://github.com/lukehaas/RunJS/tree/v1.9.0
The last time I ran this I got the result PI is 3.143409347609027