From fa0c7f349d2128ef57a878fa3fb4fd5a10bd91bd Mon Sep 17 00:00:00 2001 From: Chinmaya Krishnan Mahesh Date: Fri, 29 Jun 2018 17:07:12 +0530 Subject: [PATCH 1/4] add Go implementation of monte carlo --- chapters/monte_carlo/code/go/monteCarlo.go | 42 ++++++++++++++++++++++ chapters/monte_carlo/monte_carlo.md | 4 +++ 2 files changed, 46 insertions(+) create mode 100644 chapters/monte_carlo/code/go/monteCarlo.go diff --git a/chapters/monte_carlo/code/go/monteCarlo.go b/chapters/monte_carlo/code/go/monteCarlo.go new file mode 100644 index 000000000..06b00e60e --- /dev/null +++ b/chapters/monte_carlo/code/go/monteCarlo.go @@ -0,0 +1,42 @@ +// Submitted by Chinmaya Mahesh (chin123) + +package main + +import ( + "fmt" + "math" + "math/rand" + "time" +) + +func inCircle(x, y float64) bool { + if x*x+y*y < 1.0 { // the radius of an unit circle is 1.0 + return true + } else { + return false + } +} + +func monteCarlo(samples int) { + count := 0 + s := rand.NewSource(time.Now().UnixNano()) + r := rand.New(s) + + for i := 0; i < samples; i++ { + x, y := r.Float64(), r.Float64() + + if inCircle(x, y) { + count += 1 + } + } + + var estimate float64 + estimate = 4.0 * float64(count) / float64(samples) + + fmt.Println("The estimate of pi is", estimate) + fmt.Printf("Which has an error of %f%%\n", 100*math.Abs(math.Pi-estimate)/math.Pi) +} + +func main() { + monteCarlo(10000000) +} diff --git a/chapters/monte_carlo/monte_carlo.md b/chapters/monte_carlo/monte_carlo.md index 846dcc6f6..229caf123 100644 --- a/chapters/monte_carlo/monte_carlo.md +++ b/chapters/monte_carlo/monte_carlo.md @@ -47,6 +47,8 @@ each point is tested to see whether it's in the circle or not: [import:7-9, lang:"rust"](code/rust/monte_carlo.rs) {% sample lang="d" %} [import:2-5, lang:"d"](code/rust/monte_carlo.d) +{% sample lang="go" %} +[import:12-18, lang:"go"](code/go/monteCarlo.go) {% endmethod %} If it's in the circle, we increase an internal count by one, and in the end, @@ -93,6 +95,8 @@ Feel free to submit your version via pull request, and thanks for reading! ### D {%sample lang="d" %} [import, lang:"d"](code/d/monte_carlo.d) +{%sample lang="go" %} +[import, lang:"go"](code/go/monteCarlo.go) {% endmethod %} From 524a3afb7d10b26ca13fa041de94b9518843ed86 Mon Sep 17 00:00:00 2001 From: Chinmaya Krishnan Mahesh Date: Fri, 29 Jun 2018 17:09:40 +0530 Subject: [PATCH 2/4] Add Go heading before import in monte carlo --- chapters/monte_carlo/monte_carlo.md | 1 + 1 file changed, 1 insertion(+) diff --git a/chapters/monte_carlo/monte_carlo.md b/chapters/monte_carlo/monte_carlo.md index 229caf123..611107fa5 100644 --- a/chapters/monte_carlo/monte_carlo.md +++ b/chapters/monte_carlo/monte_carlo.md @@ -95,6 +95,7 @@ Feel free to submit your version via pull request, and thanks for reading! ### D {%sample lang="d" %} [import, lang:"d"](code/d/monte_carlo.d) +### Go {%sample lang="go" %} [import, lang:"go"](code/go/monteCarlo.go) {% endmethod %} From ced67bc7fabbd8c7e7c216d1f537188de6dd3b61 Mon Sep 17 00:00:00 2001 From: Chinmaya Krishnan Mahesh Date: Fri, 29 Jun 2018 17:47:55 +0530 Subject: [PATCH 3/4] Update monteCarlo.go --- chapters/monte_carlo/code/go/monteCarlo.go | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/chapters/monte_carlo/code/go/monteCarlo.go b/chapters/monte_carlo/code/go/monteCarlo.go index 06b00e60e..bea71ef41 100644 --- a/chapters/monte_carlo/code/go/monteCarlo.go +++ b/chapters/monte_carlo/code/go/monteCarlo.go @@ -10,11 +10,7 @@ import ( ) func inCircle(x, y float64) bool { - if x*x+y*y < 1.0 { // the radius of an unit circle is 1.0 - return true - } else { - return false - } + return x*x+y*y < 1.0 // the radius of an unit circle is 1.0 } func monteCarlo(samples int) { @@ -29,9 +25,8 @@ func monteCarlo(samples int) { count += 1 } } - - var estimate float64 - estimate = 4.0 * float64(count) / float64(samples) + + estimate := 4.0 * float64(count) / float64(samples) fmt.Println("The estimate of pi is", estimate) fmt.Printf("Which has an error of %f%%\n", 100*math.Abs(math.Pi-estimate)/math.Pi) From 9edf0ef367339ea7281f6c048be834d9765e02ca Mon Sep 17 00:00:00 2001 From: Chinmaya Krishnan Mahesh Date: Fri, 29 Jun 2018 17:48:23 +0530 Subject: [PATCH 4/4] Update monte_carlo.md --- chapters/monte_carlo/monte_carlo.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chapters/monte_carlo/monte_carlo.md b/chapters/monte_carlo/monte_carlo.md index 611107fa5..bbd33671f 100644 --- a/chapters/monte_carlo/monte_carlo.md +++ b/chapters/monte_carlo/monte_carlo.md @@ -48,7 +48,7 @@ each point is tested to see whether it's in the circle or not: {% sample lang="d" %} [import:2-5, lang:"d"](code/rust/monte_carlo.d) {% sample lang="go" %} -[import:12-18, lang:"go"](code/go/monteCarlo.go) +[import:12-14, lang:"go"](code/go/monteCarlo.go) {% endmethod %} If it's in the circle, we increase an internal count by one, and in the end,