| Home | Research | Classes | Astronomy | Sporting Clays | Affiliations | My Library | Links |
I was inspired to post this by Rhett Allain's work on calculating Pi based upon a random walk.
Another method that you can use to calculate Pi based upon random numbers relies upon Monte Carlo Integration of a Unit Circle. Here is a Mac OS X 10.10 and higher executable that you can use to explore the calculation. Increasing the number of guesses, gives you are better value of Pi within the limits of computational error.
You can download the Swift 3.0 code for this project here. Two Functions perform the bulk of the work and are reproduced below. The first calculates the value of Pi by taking the ratio of the number of random points within the radius of a Unit Sphere to those outside the radius and multiplying by the area of a bounding box. The second function calculates the total number of points within the the Unit Sphere. |
![]() |
Using random numbers to calculate the value of pi by determining the area of a Unit Circle where the Area = pi.
|
| This software is released under an MIT License. |
Copyright (c) 2017 Jeff Terry
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. |