Skip to content

Added Monte Carlo in Swift 4.1 #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 2, 2018
Merged

Added Monte Carlo in Swift 4.1 #197

merged 4 commits into from
Jul 2, 2018

Conversation

Wesley-Arrington
Copy link
Contributor

Added Monte Carlo in Swift 4.1

Added Monte Carlo in Swift 4.1
@june128 june128 added the Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) label Jul 2, 2018
@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're missing {% sample lang="swift" %} here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about that just committed a change to fix it


func isInCircle(x: Double, y: Double, radius: Double) -> Bool {

return (x*x) + (y*y) < Double(radius*radius)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to cast Double(radius*radius) even though you have radius: Double ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're totally right I just committed a change to remove that redundancy



func main() {
print("Pi estimate is: ", monteCarlo(n: 10000, radius: 50))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was a messy non-linear process, but we've moved away from using radius as a free parameter, since it the result doesn't depend on the choice of radius at all. Lose radius from the monteCarlo function and hardcode the radius to 1 inside.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good I just updated my code accordingly 👍

}

let piEstimate = Double(4 * piCount)/(Double(n))
print("Percent error is: \(100*(Double.pi - piEstimate)/Double.pi)%")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the error testing to main()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion placing it in main() makes more sense. I just made a commit which addresses this.


public static var random: Double {

return Double(arc4random()) / 0xFFFFFFFF
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does that do? The hardcoded 0xFFFFFFFF makes me uneasy :)
Maybe add a comment here?

Copy link
Contributor Author

@Wesley-Arrington Wesley-Arrington Jul 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good added a comment which says what that line of code does

@jiegillet
Copy link
Member

Sorry for the back and forth, I had written a review but had not realized it hadn't been shared yet!

- Added comment
- Removed radius as a free parameter for the monte carlo function
- Moved error testing to main()
@jiegillet
Copy link
Member

Thank you, looks great now :)

@jiegillet jiegillet merged commit a9ef5ff into algorithm-archivists:master Jul 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants