Skip to content

Add D implementation of monte carlo #162

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 2 commits into from
Jun 29, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 29 additions & 0 deletions chapters/monte_carlo/code/d/monte_carlo.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
///Returns true if a point (x, y) is in the circle with radius r
bool inCircle(double x, double y, double r = 1.0)
{
return x ^^ 2 + y ^^ 2 < r ^^ 2;
}

///Calculate pi using monte carlo
real monteCarloPI(ulong n)
{
import std.algorithm : count;
import std.random : Random, uniform01, unpredictableSeed;
import std.range : generate, take;
import std.typecons : tuple;

auto rnd = Random(unpredictableSeed);
return generate(() => tuple!("x", "y")(rnd.uniform01, rnd.uniform01))
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should make this part a little more explicit, something like:

    auto radius = 1.0;
    auto piCount = generate(() => tuple!("x", "y")(rnd.uniform01, rnd.uniform01))
        .take(n)
        .count!(a => inCircle(a.x, a.y, radius));
    return 4.0 * piCount / (n * radius * radius);

I definitely think radius should be a variable just or clarity's sake and with that change, jamming the return value into one expression seems a bit over the top.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can do the return bit, but its worth noting that the Julia code is being refactored to remove the radius variable entirely. I think that would be better.

Copy link
Contributor

Choose a reason for hiding this comment

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

Removing radius also works but I'd still introduce the separate variable for piCount.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

.take(n)
.count!(a => inCircle(a.x, a.y)) / cast(real) n * 4.0;
}

void main()
{
import std.math : abs, PI;
import std.stdio : writeln;

auto p = monteCarloPI(100_000);
writeln("Estimated pi: ", p);
writeln("Percent error: ", abs(p - PI) * 100 / PI);
}
5 changes: 5 additions & 0 deletions chapters/monte_carlo/monte_carlo.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ each point is tested to see whether it's in the circle or not:
[import:7-7, lang:"haskell"](code/haskell/monteCarlo.hs)
{% sample lang="rs" %}
[import:7-9, lang:"rust"](code/rust/monte_carlo.rs)
{% sample lang="d" %}
[import:2-5, lang:"d"](code/rust/monte_carlo.d)
{% endmethod %}

If it's in the circle, we increase an internal count by one, and in the end,
Expand Down Expand Up @@ -88,6 +90,9 @@ Feel free to submit your version via pull request, and thanks for reading!
{%sample lang="rs" %}
### Rust
[import, lang:"rust"](code/rust/monte_carlo.rs)
### D
{%sample lang="d" %}
[import, lang:"d"](code/d/monte_carlo.d)
{% endmethod %}


Expand Down