-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add example of estimating pi using Monte Carlo simulation to std::rand #16362
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
Conversation
//! use std::rand; | ||
//! use std::rand::distributions::{IndependentSample, Range}; | ||
//! | ||
//! fn dist(x: f64, y: f64) -> f64 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is more efficient as x * x + y * y <= r * r
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@huonw beat me to it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I've removed the use of sqrt.
I really like this example. Concise, clear, simple. But I'm suuuper biased since my field is computational geometry. I think my biggest issue with this is that it only uses one kind of RNG in one way, while the monty-hall example does two different RNG operations (although you really need to stare at it for a bit to figure them out). That is of course assuming we're talking about having only one-or-the-other. If we want both, then I think this is a great "easy mode" example to start with. |
I'm not entirely sure if it's good to have one example, or both. And if it's only one, which it is. I think I'm fine with either merge or close, leaning slightly towards merge. |
After giving it more thought, two examples are better than one. If it feels like too much, we can remove one, but we're still feeling out how much is appropriate for a module-level doc, so it's good to try some edge cases out. |
The failure seems unrelated to my PR? |
//! circle, both centered at the origin. Since the area of a unit circle is pi, | ||
//! the ratio | ||
//! | ||
//! (area of unit circle) / (area of square) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be
```notrust
(area of unit circle) / (area of square)
```
because rustdoc defaults to running code blocks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahhhhhhhhhh
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doh. Thanks, fixed.
//! the ratio | ||
//! | ||
//! ```notrust | ||
//! (area of unit circle) / (area of square) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want this to have the leading four spaces in the rendered output?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, this could be phrased as just
Since the area of the unit circle is π, we have
(area of unit circle) / (area of square) = π/4
So if ...
Pros: I like this example because it's concise without being trivial. The Monty Hall example code is somewhat lengthy and possibly inaccessible to those unfamiliar with probability. Cons: The Monty Hall example already exists. Do we need another example? Also, this is probably inaccessible to people who don't know basic geometry.
Pros:
I like this example because it's concise without being trivial. The Monty Hall example code is somewhat lengthy and possibly inaccessible to those unfamiliar with probability.
Cons:
The Monty Hall example already exists. Do we need another example? Also, this is probably inaccessible to people who don't know basic geometry.