Skip to content

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

Merged
merged 4 commits into from
Aug 7, 2018
Merged

Implement C++ version of Monte Carlo integration #342

merged 4 commits into from
Aug 7, 2018

Conversation

bsamseth
Copy link
Contributor

@bsamseth bsamseth commented Aug 6, 2018

I just came across this project and wanted to chip in something. Hope this conforms to any style preferences. 👍

@Gathros Gathros added the Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) label Aug 6, 2018
++count;
}

return 4.0 * count / (double) samples;
Copy link
Member

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

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

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

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.

mika314
mika314 previously approved these changes Aug 7, 2018
Copy link
Contributor

@mika314 mika314 left a 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)
Copy link
Member

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 :)

Copy link
Contributor Author

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!

Copy link
Member

Choose a reason for hiding this comment

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

Super quick!

Copy link
Member

@jiegillet jiegillet left a 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 :)

@jiegillet jiegillet merged commit dba1280 into algorithm-archivists:master Aug 7, 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.

5 participants