Skip to content

added monte carlo method c# #239

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

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ GuyPozner
<br>
William Boyles
<br>
sanstorik
40 changes: 40 additions & 0 deletions chapters/monte_carlo/code/csharp/MonteCarlo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// submitted by vitalii shvetsov (@sanstorik)
using System;
using System.Collections.Generic;
using System.Text;

namespace MonteCarlo
{
class MonteCarlo
{
private static Random random = new Random(47);
private int Radius { get; set; }
private int Samples { get; set; }


public MonteCarlo(int radius = 1, int samples = 1000000)
{
Radius = radius;
Samples = samples;
}


public double Run()
{
int piCount = 0;

for (int i = 0; i < Samples; i++)
{
var randomX = random.NextDouble() * Radius;
var randomY = random.NextDouble() * Radius;

if (IsInCircle(randomX, randomY)) piCount++;
}

return 4 * piCount / (double)Samples;
}


private bool IsInCircle(double x, double y) => x * x + y * y < Radius * Radius;
}
}
18 changes: 18 additions & 0 deletions chapters/monte_carlo/code/csharp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

// submitted by vitalii shvetsov (@sanstorik)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe move this at the very top?

namespace MonteCarlo
{
class Program
{
public static void Main(string[] args)
{
double piEstimate = new MonteCarlo(1, 1000000).Run();

Console.WriteLine("Pi estimate = {0}", piEstimate);
Console.WriteLine("Pi error = {0}", 100 * (Math.Abs(Math.PI - piEstimate)) / Math.PI);

Console.ReadLine();
}
}
}
5 changes: 5 additions & 0 deletions chapters/monte_carlo/monte_carlo.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ each point is tested to see whether it's in the circle or not:
[import:13-15, lang:"java"](code/java/MonteCarlo.java)
{% sample lang="swift" %}
[import:21-25, lang:"swift"](code/swift/monte_carlo.swift)
{% sample lang="csharp" %}
[import:37-39, lang:"csharp"](code/csharp/MonteCarlo.cs)
Copy link
Member

Choose a reason for hiding this comment

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

The code import should only be line 38, since you made the method smaller.

{% endmethod %}


If it's in the circle, we increase an internal count by one, and in the end,

$$
Expand Down Expand Up @@ -123,6 +126,8 @@ Feel free to submit your version via pull request, and thanks for reading!
{% sample lang="swift" %}
### Swift
[import, lang:"swift"](code/swift/monte_carlo.swift)
### CSharp
Copy link
Member

Choose a reason for hiding this comment

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

You need to put {% sample lang="csharp" %} above this line, so that we see the code when selecting C#.

[import, lang:"csharp"](code/csharp/MonteCarlo.cs)
{% endmethod %}


Expand Down