Generating pi (Ï€) using random numbers

I was entertained by Numberphile’s dice-based generation of an approximate of pi  so thought I’d throw together a quick script to try this out:

random_vals_pi

The results look pretty good:

$ for x in 1 2 3 4 5 ; do python random_vals_pi.py 100000000 100000; done
3.14130098162
3.13774230925
3.14174020697
3.13769082316
3.14274853819

The theory is based on having large integers, so reducing this reduces the accuracy of the approximation:

$ for x in 1 2 3 4 5 ; do python random_vals_pi.py 10 100000; done
3.07962120525
3.0938112197
3.08601801522
3.08910857173
3.08599352408

And similarly reducing the number of samples does the same:

$ for x in 1 2 3 4 5 ; do python random_vals_pi.py 100000000 10; done
2.92770021885
4.472135955
3.46410161514
2.92770021885
3.46410161514

 

Leave a Reply

Your email address will not be published. Required fields are marked *