Monte Carlo Pi
Published: March 1, 2021Edited: March 22, 2021
Using randomness to solve problems that might be deterministic in principle. It is useful in circumstances when there is a simple test to verify if some solution is valid or invalid, many guesses are (uniformly) generated in the potential domain and their validity checked, then the proportion of valid guesses approximates to the solution.
Examples
This is a single threaded approximation of the value of π. It can calculate a trillion guesses in around ten seconds, the multithreaded version on my four core ThinkPad performs the same amount of guesses in around five seconds. An impressive loss of efficiency…
Here we ride the monoid, using some properties of the “unit” or “identity”
element under multiplication to relate the unit square and unit circle to find
π. Which is to say we’re using the number one a lot because 1*1 = 1
.
|
|