Skip to content

Add Monte Carlo Integration in C# #309

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 2, 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
25 changes: 25 additions & 0 deletions contents/monte_carlo_integration/code/csharp/Circle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;

namespace MonteCarloIntegration
{
public struct Point
{
public double X { get; set; }
public double Y { get; set; }

public Point(double x, double y)
{
this.X = x;
this.Y = y;
}
}

public class Circle
{
public double Radius { get; private set; }

public Circle(double radius) => this.Radius = Math.Abs(radius);

public bool IsInMe(Point point) => Math.Pow(point.X, 2) + Math.Pow(point.Y, 2) < Math.Pow(Radius, 2);
}
}
23 changes: 23 additions & 0 deletions contents/monte_carlo_integration/code/csharp/MonteCarlo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;

namespace MonteCarloIntegration
{
public class MonteCarlo
{
public double Run(int samples)
{
var circle = new Circle(1.0);
var count = 0;
var random = new Random();

for (int i = 0; i < samples; i++)
{
var point = new Point(random.NextDouble(), random.NextDouble());
if (circle.IsInMe(point))
count++;
}

return 4.0 * count / samples;
}
}
}
16 changes: 16 additions & 0 deletions contents/monte_carlo_integration/code/csharp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

namespace MonteCarloIntegration
{
class Program
{
static void Main(string[] args)
{
var monteCarlo = new MonteCarlo();
System.Console.WriteLine("Running with 10,000,000 samples.");
var piEstimate = monteCarlo.Run(10000000);
System.Console.WriteLine($"The estimate of pi is: {piEstimate}");
System.Console.WriteLine($"The percent error is: {Math.Abs(piEstimate - Math.PI) / Math.PI * 100}%");
}
}
}
9 changes: 9 additions & 0 deletions contents/monte_carlo_integration/monte_carlo_integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ each point is tested to see whether it's in the circle or not:
[import:15-17, lang:"swift"](code/swift/monte_carlo.swift)
{% sample lang="py" %}
[import:5-7, lang:"python"](code/python/monte_carlo.py)
{% sample lang="cs" %}
[import:23-23, lang:"csharp"](code/csharp/Circle.cs)
{% endmethod %}

If it's in the circle, we increase an internal count by one, and in the end,
Expand Down Expand Up @@ -116,6 +118,13 @@ Feel free to submit your version via pull request, and thanks for reading!
[import, lang:"swift"](code/swift/monte_carlo.swift)
{% sample lang="py" %}
[import, lang:"python"](code/python/monte_carlo.py)
{% sample lang="cs" %}
MonteCarlo.cs
[import, lang:"csharp"](code/csharp/MonteCarlo.cs)
Circle.cs
[import, lang:"csharp"](code/csharp/Circle.cs)
Program.cs
[import, lang:"csharp"](code/csharp/Program.cs)
{% endmethod %}


Expand Down