-
-
Notifications
You must be signed in to change notification settings - Fork 360
Implement C++ version of Monte Carlo integration #342
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
++count; | ||
} | ||
|
||
return 4.0 * count / (double) samples; |
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 cast isn't necessary.
std::cout << "Enter samples to use: "; | ||
std::cin >> samples; | ||
|
||
double pi = monte_carlo_pi(samples); |
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.
Could you name this something else? The difference between lowercase and uppercase pi isn't enough to differentiate where they come from.
std::cin >> samples; | ||
|
||
double pi = monte_carlo_pi(samples); | ||
printf("Pi = %f\n", pi); |
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.
Can you use std::cout
here and the next line?
*/ | ||
double monte_carlo_pi(unsigned samples) { | ||
static std::default_random_engine generator; | ||
static std::uniform_real_distribution<double> dist(-1, 1); |
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.
All the other implementation use a distribution should be between 0 and 1 since we are only looking into a corner. It doesn't fundamentally change anything since you only ever consider x*x
anyway, but it would be more consistent with the chapter.
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.
+1 to existing comments, everything else looks good
double x = dist(generator); | ||
double y = dist(generator); | ||
|
||
if (x*x + y*y < 1) |
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.
Wait, I just realized you are not using the in_circle
function. That defeats the purpose of defining 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.
Your right! My bad, fixed this now!
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.
Super quick!
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 a lot for a fine first contribution to the AAA!
We hope more are coming :)
I just came across this project and wanted to chip in something. Hope this conforms to any style preferences. 👍