Skip to content

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

Merged
merged 4 commits into from
Aug 21, 2014

Conversation

nham
Copy link
Contributor

@nham nham commented Aug 8, 2014

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.

//! use std::rand;
//! use std::rand::distributions::{IndependentSample, Range};
//!
//! fn dist(x: f64, y: f64) -> f64 {
Copy link
Member

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.

Copy link
Contributor

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!

Copy link
Contributor Author

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.

@Gankra
Copy link
Contributor

Gankra commented Aug 9, 2014

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.

@alexcrichton
Copy link
Member

cc @steveklabnik

@steveklabnik
Copy link
Member

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.

@steveklabnik
Copy link
Member

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.

@nham
Copy link
Contributor Author

nham commented Aug 21, 2014

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)
Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

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

Ahhhhhhhhhh

Copy link
Contributor Author

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)
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 want this to have the leading four spaces in the rendered output?

Copy link
Member

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 ...

bors added a commit that referenced this pull request Aug 21, 2014
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.
@bors bors closed this Aug 21, 2014
@bors bors merged commit 8658722 into rust-lang:master Aug 21, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants