The Math Pi value is related to Circle. If we have a circle inside a square, the ration of area between circle and the square is:
Thus, if we have a good-enough psuedo random generator, we can generate as many points (x-y coordinate) as possible, and then count those who are inside the circle (by checking the distance between it to (0, 0) origin), and then count total points we have generated.
In GoLang, we can use math/rand’s rand.Float64 to generate a random number at range [0, 1.0).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package main import ( "fmt" "math/rand" "os" "strconv" ) func compute_pi(n int) float64 { var total int = 0 var totalIn int = 0 for i := 0; i < n; i++ { var x = rand.Float64() var y = rand.Float64() if x*x+y*y < 1 { totalIn++ } total++ } return float64(totalIn) * 4.0 / float64(total) } func main() { var n = 100000 if len(os.Args) == 2 { intVar, err := strconv.Atoi(os.Args[1]) if err == nil { n = intVar } } fmt.Println("Pi is:", compute_pi(n)) } |
package main import ( "fmt" "math/rand" "os" "strconv" ) func compute_pi(n int) float64 { var total int = 0 var totalIn int = 0 for i := 0; i < n; i++ { var x = rand.Float64() var y = rand.Float64() if x*x+y*y < 1 { totalIn++ } total++ } return float64(totalIn) * 4.0 / float64(total) } func main() { var n = 100000 if len(os.Args) == 2 { intVar, err := strconv.Atoi(os.Args[1]) if err == nil { n = intVar } } fmt.Println("Pi is:", compute_pi(n)) }
We can pass the number of iterations we want like this:
1 2 3 4 5 6 7 8 9 10 | $ go run pi.go 1000 Pi is: 3.168 $ go run pi.go 10000 Pi is: 3.16 $ go run pi.go 100000 Pi is: 3.1518 $ go run pi.go 1000000 Pi is: 3.140552 $ go run pi.go 10000000 Pi is: 3.1414072 |
$ go run pi.go 1000 Pi is: 3.168 $ go run pi.go 10000 Pi is: 3.16 $ go run pi.go 100000 Pi is: 3.1518 $ go run pi.go 1000000 Pi is: 3.140552 $ go run pi.go 10000000 Pi is: 3.1414072
Monte Carlo Simulation Algorithms to compute the Pi based on Randomness:
- Teaching Kids Programming – Area and Circumferences of Circle and Monte Carlo Simulation Algorithm of PI
- Using Parallel For in Java to Compute PI using Monte Carlo Algorithm
- Monte Carlo solution for Mathematics × Programming Competition #7
- R Programming Tutorial – How to Compute PI using Monte Carlo in R?
- C++ Coding Exercise – Parallel For – Monte Carlo PI Calculation
- Area of the Shadow? – The Monte Carlo Solution in VBScript
- VBScript Coding Exercise – Compute PI using Monte Carlo Random Method
- Computing Approximate Value of PI using Monte Carlo in Python
- How does the 8-bit BASIC perform on Famicom Clone – Subor SB2000 – FBasic – Compute PI approximation using Monte-Carlo method
- GoLang: Compute the Math Pi Value via Monte Carlo Simulation
- BASH Script to Compute the Math.PI constant via Monte Carlo Simulation
–EOF (The Ultimate Computing & Technology Blog) —
GD Star Rating
loading...
711 wordsloading...
Last Post: Teaching Kids Programming - Breadth First Search Algorithm to Compute the Max Width of a Binary Tree
Next Post: Teaching Kids Programming - Depth First Search Algorithm to Compute the Max Width of a Binary Tree