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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,6 @@ Arun Sahadeo
<br>
NIFR91
<br>
Michal Hanajik
Michal Hanajik
<br>
Bendik Samseth
48 changes: 48 additions & 0 deletions contents/monte_carlo_integration/code/c++/monte_carlo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <iostream>
#include <cstdlib>
#include <random>

constexpr double PI = 3.14159265358979323846264338;

/**
* Check if the point (x, y) is within a circle of a given radius.
* @param x coordinate one
* @param y coordinate two
* @param r radius of the circle (optional)
* @return true if (x, y) is within the circle.
*/
inline bool in_circle(double x, double y, double r = 1) {
return x * x + y * y < r * r;
}

/**
* Return an estimate of PI using Monte Carlo integration.
* @param samples number of iterations to use
* @return estimate of pi
*/
double monte_carlo_pi(unsigned samples) {
static std::default_random_engine generator;
static std::uniform_real_distribution<double> dist(0, 1);

unsigned count = 0;
for (unsigned i = 0; i < samples; ++i) {
double x = dist(generator);
double y = dist(generator);

if (in_circle(x, y))
++count;
}

return 4.0 * count / samples;
}

int main() {
unsigned samples;

std::cout << "Enter samples to use: ";
std::cin >> samples;

double pi_estimate = monte_carlo_pi(samples);
std::cout << "Pi = " << pi_estimate << '\n';
std::cout << "Percent error is: " << 100 * std::abs(pi_estimate - PI) / PI << " %\n";
}
4 changes: 4 additions & 0 deletions contents/monte_carlo_integration/monte_carlo_integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ each point is tested to see whether it's in the circle or not:
[import:3-10, lang:"clojure"](code/clojure/monte_carlo.clj)
{% sample lang="c" %}
[import:7-9, lang:"c_cpp"](code/c/monte_carlo.c)
{% sample lang="cpp" %}
[import:7-16, lang:"c_cpp"](code/c++/monte_carlo.cpp)
{% sample lang="js" %}
[import:2-6, lang:"javascript"](code/javascript/monte_carlo.js)
{% sample lang="hs" %}
Expand Down Expand Up @@ -100,6 +102,8 @@ Feel free to submit your version via pull request, and thanks for reading!
[import, lang:"clojure"](code/clojure/monte_carlo.clj)
{% sample lang="c" %}
[import, lang:"c_cpp"](code/c/monte_carlo.c)
{% sample lang="cpp" %}
[import, lang:"c_cpp"](code/c++/monte_carlo.cpp)
{% sample lang="js" %}
[import, lang:"javascript"](code/javascript/monte_carlo.js)
{% sample lang="hs" %}
Expand Down