From 9788f19f59f4abd4032374434f6d11f2af1b51d9 Mon Sep 17 00:00:00 2001 From: jasmaa Date: Sat, 25 Aug 2018 21:09:55 -0400 Subject: [PATCH 1/3] Implement monte carlo in ruby --- .../code/ruby/monte_carlo.rb | 31 +++++++++++++++++++ .../monte_carlo_integration.md | 4 +++ 2 files changed, 35 insertions(+) create mode 100644 contents/monte_carlo_integration/code/ruby/monte_carlo.rb diff --git a/contents/monte_carlo_integration/code/ruby/monte_carlo.rb b/contents/monte_carlo_integration/code/ruby/monte_carlo.rb new file mode 100644 index 000000000..e3511b80b --- /dev/null +++ b/contents/monte_carlo_integration/code/ruby/monte_carlo.rb @@ -0,0 +1,31 @@ +def in_circle(x, y, radius=1) + # Check if coords are in circle via Pythagorean Thm + return (x*x + y*y) < radius*radius +end + +def monte_carlo(n_samples, radius=1) + # estimate pi via monte carlo sampling + in_circle_count = 0.0 + + for _ in 0...n_samples + # randomly choose coords within square + x = rand()*radius + y = rand()*radius + if in_circle(x, y, radius) + in_circle_count += 1 + end + end + + # circle area is pi*r^2 and rect area is 4r^2 + # ratio between the two is then pi/4 so multiply by 4 to get pi + return 4 * (in_circle_count / n_samples) + +end + + +# Main +pi_estimate = monte_carlo(100000) +percent_error = 100 * (pi_estimate - Math::PI).abs + +puts "The estimate of pi is: #{pi_estimate.round(3)}" +puts "The percent error is: #{percent_error.round(3)}" \ No newline at end of file diff --git a/contents/monte_carlo_integration/monte_carlo_integration.md b/contents/monte_carlo_integration/monte_carlo_integration.md index 55fadf9c6..a56b956ba 100644 --- a/contents/monte_carlo_integration/monte_carlo_integration.md +++ b/contents/monte_carlo_integration/monte_carlo_integration.md @@ -67,6 +67,8 @@ each point is tested to see whether it's in the circle or not: [import:23-23, lang:"csharp"](code/csharp/Circle.cs) {% sample lang="nim" %} [import:6-7, lang:"nim"](code/nim/monte_carlo.nim) +{% sample lang="ruby" %} +[import:1-4, lang:"ruby"](code/ruby/monte_carlo.rb) {% endmethod %} If it's in the circle, we increase an internal count by one, and in the end, @@ -133,6 +135,8 @@ Feel free to submit your version via pull request, and thanks for reading! [import, lang:"csharp"](code/csharp/Program.cs) {% sample lang="nim" %} [import, lang:"nim"](code/nim/monte_carlo.nim) +{% sample lang="ruby" %} +[import, lang:"ruby"](code/ruby/monte_carlo.rb) {% endmethod %} From 2e738b8293005ef126b2627c2b599e0158bb44d0 Mon Sep 17 00:00:00 2001 From: jasmaa Date: Sat, 1 Sep 2018 10:42:04 -0400 Subject: [PATCH 2/3] Fix percent error --- contents/monte_carlo_integration/code/ruby/monte_carlo.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contents/monte_carlo_integration/code/ruby/monte_carlo.rb b/contents/monte_carlo_integration/code/ruby/monte_carlo.rb index e3511b80b..b6bbe1d35 100644 --- a/contents/monte_carlo_integration/code/ruby/monte_carlo.rb +++ b/contents/monte_carlo_integration/code/ruby/monte_carlo.rb @@ -25,7 +25,7 @@ def monte_carlo(n_samples, radius=1) # Main pi_estimate = monte_carlo(100000) -percent_error = 100 * (pi_estimate - Math::PI).abs +percent_error = 100 * (pi_estimate - Math::PI).abs / Math::PI puts "The estimate of pi is: #{pi_estimate.round(3)}" -puts "The percent error is: #{percent_error.round(3)}" \ No newline at end of file +puts "The percent error is: #{percent_error.round(3)}" From 2e162029b8bdd5075a5d69a9d289fe46b378e6ac Mon Sep 17 00:00:00 2001 From: jasmaa Date: Sat, 1 Sep 2018 10:46:28 -0400 Subject: [PATCH 3/3] new line --- contents/monte_carlo_integration/code/ruby/monte_carlo.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/contents/monte_carlo_integration/code/ruby/monte_carlo.rb b/contents/monte_carlo_integration/code/ruby/monte_carlo.rb index b6bbe1d35..406aac798 100644 --- a/contents/monte_carlo_integration/code/ruby/monte_carlo.rb +++ b/contents/monte_carlo_integration/code/ruby/monte_carlo.rb @@ -29,3 +29,4 @@ def monte_carlo(n_samples, radius=1) puts "The estimate of pi is: #{pi_estimate.round(3)}" puts "The percent error is: #{percent_error.round(3)}" +