From 4895edccdfede3d51512a9afc50fc03167359813 Mon Sep 17 00:00:00 2001 From: CD Sigma Date: Sun, 1 Jul 2018 17:19:51 -0700 Subject: [PATCH 1/4] Added Monte Carlo in Swift 4.1 Added Monte Carlo in Swift 4.1 --- .../monte_carlo/code/swift/monte_carlo.swift | 54 +++++++++++++++++++ chapters/monte_carlo/monte_carlo.md | 4 ++ 2 files changed, 58 insertions(+) create mode 100644 chapters/monte_carlo/code/swift/monte_carlo.swift diff --git a/chapters/monte_carlo/code/swift/monte_carlo.swift b/chapters/monte_carlo/code/swift/monte_carlo.swift new file mode 100644 index 000000000..051d420a0 --- /dev/null +++ b/chapters/monte_carlo/code/swift/monte_carlo.swift @@ -0,0 +1,54 @@ +//Double Extension from YannickSteph on StackOverflow: https://stackoverflow.com/questions/25050309/swift-random-float-between-0-and-1 + +import Foundation + + + +public extension Double { + + public static var random: Double { + + return Double(arc4random()) / 0xFFFFFFFF + } + + public static func random(min: Double, max: Double) -> Double { + + return Double.random * (max - min) + min + } +} + + +func isInCircle(x: Double, y: Double, radius: Double) -> Bool { + + return (x*x) + (y*y) < Double(radius*radius) +} + + +func monteCarlo(n: Int, radius: Double) -> Double { + + var piCount = 0 + var randX: Double + var randY: Double + + for _ in 0...n { + randX = Double.random(min: 0, max: radius) + randY = Double.random(min: 0, max: radius) + + if(isInCircle(x: randX, y: randY, radius: radius)) { + piCount += 1 + } + } + + let piEstimate = Double(4 * piCount)/(Double(n)) + print("Percent error is: \(100*(Double.pi - piEstimate)/Double.pi)%") + + return piEstimate +} + + +func main() { + print("Pi estimate is: ", monteCarlo(n: 10000, radius: 50)) +} + + +main() diff --git a/chapters/monte_carlo/monte_carlo.md b/chapters/monte_carlo/monte_carlo.md index 2eb2d2e4d..51c9db56d 100644 --- a/chapters/monte_carlo/monte_carlo.md +++ b/chapters/monte_carlo/monte_carlo.md @@ -55,6 +55,8 @@ each point is tested to see whether it's in the circle or not: [import:12-14, lang:"golang"](code/go/monteCarlo.go) {% sample lang="java" %} [import:11-13, lang:"java"](code/java/MonteCarlo.java) +{% sample lang="swift" %} +[import:21-25, lang:"swift"](code/swift/monte_carlo.swift) {% endmethod %} If it's in the circle, we increase an internal count by one, and in the end, @@ -113,6 +115,8 @@ Feel free to submit your version via pull request, and thanks for reading! {% sample lang="java" %} ### Java [import, lang:"java"](code/java/MonteCarlo.java) +### Swift +[import, lang:"swift"](code/java/monte_carlo.swift) {% endmethod %} From 2a41b2ecfff58bbff9afbbf25509cdfcbdf5fe85 Mon Sep 17 00:00:00 2001 From: CD Sigma Date: Sun, 1 Jul 2018 20:55:36 -0700 Subject: [PATCH 2/4] Cleaning up Code Removed unnecessary cast to Double --- chapters/monte_carlo/code/swift/monte_carlo.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chapters/monte_carlo/code/swift/monte_carlo.swift b/chapters/monte_carlo/code/swift/monte_carlo.swift index 051d420a0..3bb2adf28 100644 --- a/chapters/monte_carlo/code/swift/monte_carlo.swift +++ b/chapters/monte_carlo/code/swift/monte_carlo.swift @@ -20,7 +20,7 @@ public extension Double { func isInCircle(x: Double, y: Double, radius: Double) -> Bool { - return (x*x) + (y*y) < Double(radius*radius) + return (x*x) + (y*y) < radius*radius } From 5282c84c56dbba5b20750052e89b53b668907804 Mon Sep 17 00:00:00 2001 From: CD Sigma Date: Sun, 1 Jul 2018 20:56:34 -0700 Subject: [PATCH 3/4] Adding missing line of code --- 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 51c9db56d..3a04fbdbe 100644 --- a/chapters/monte_carlo/monte_carlo.md +++ b/chapters/monte_carlo/monte_carlo.md @@ -115,6 +115,7 @@ Feel free to submit your version via pull request, and thanks for reading! {% sample lang="java" %} ### Java [import, lang:"java"](code/java/MonteCarlo.java) +{% sample lang="swift" %} ### Swift [import, lang:"swift"](code/java/monte_carlo.swift) {% endmethod %} From d95328416e16e1a14b62dc1d3d7382eb72d336d6 Mon Sep 17 00:00:00 2001 From: CD Sigma Date: Sun, 1 Jul 2018 22:52:21 -0700 Subject: [PATCH 4/4] Code Updates - Added comment - Removed radius as a free parameter for the monte carlo function - Moved error testing to main() --- .../monte_carlo/code/swift/monte_carlo.swift | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/chapters/monte_carlo/code/swift/monte_carlo.swift b/chapters/monte_carlo/code/swift/monte_carlo.swift index 3bb2adf28..29c21f953 100644 --- a/chapters/monte_carlo/code/swift/monte_carlo.swift +++ b/chapters/monte_carlo/code/swift/monte_carlo.swift @@ -5,50 +5,53 @@ import Foundation public extension Double { - + public static var random: Double { - return Double(arc4random()) / 0xFFFFFFFF + return Double(arc4random()) / 0xFFFFFFFF // Returns a random double between 0.0 and 1.0, inclusive. } - + public static func random(min: Double, max: Double) -> Double { - + return Double.random * (max - min) + min } } func isInCircle(x: Double, y: Double, radius: Double) -> Bool { - + return (x*x) + (y*y) < radius*radius } -func monteCarlo(n: Int, radius: Double) -> Double { - +func monteCarlo(n: Int) -> Double { + + let radius: Double = 1 var piCount = 0 var randX: Double var randY: Double - + for _ in 0...n { randX = Double.random(min: 0, max: radius) randY = Double.random(min: 0, max: radius) - + if(isInCircle(x: randX, y: randY, radius: radius)) { piCount += 1 } } let piEstimate = Double(4 * piCount)/(Double(n)) - print("Percent error is: \(100*(Double.pi - piEstimate)/Double.pi)%") - + return piEstimate } func main() { - print("Pi estimate is: ", monteCarlo(n: 10000, radius: 50)) + let piEstimate = monteCarlo(n: 10000) + print("Pi estimate is: ", piEstimate) + print("Percent error is: \(100*(Double.pi - piEstimate)/Double.pi)%") } main() +