Skip to content

Commit 7caac1c

Browse files
authored
Add Monte Carlo Integration in C# (#309)
* Add Monte Carlo Integration. * Rename RunOutput to PiEstimate and adjust variables accordingly. * Use Math.Pow. Use commas instead of points to make 10,000,000 more readable. Use percent error. Remove user input. * Calculate the percent error in Main. Use the absolute percent error.
1 parent 2b2b5c3 commit 7caac1c

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
3+
namespace MonteCarloIntegration
4+
{
5+
public struct Point
6+
{
7+
public double X { get; set; }
8+
public double Y { get; set; }
9+
10+
public Point(double x, double y)
11+
{
12+
this.X = x;
13+
this.Y = y;
14+
}
15+
}
16+
17+
public class Circle
18+
{
19+
public double Radius { get; private set; }
20+
21+
public Circle(double radius) => this.Radius = Math.Abs(radius);
22+
23+
public bool IsInMe(Point point) => Math.Pow(point.X, 2) + Math.Pow(point.Y, 2) < Math.Pow(Radius, 2);
24+
}
25+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
3+
namespace MonteCarloIntegration
4+
{
5+
public class MonteCarlo
6+
{
7+
public double Run(int samples)
8+
{
9+
var circle = new Circle(1.0);
10+
var count = 0;
11+
var random = new Random();
12+
13+
for (int i = 0; i < samples; i++)
14+
{
15+
var point = new Point(random.NextDouble(), random.NextDouble());
16+
if (circle.IsInMe(point))
17+
count++;
18+
}
19+
20+
return 4.0 * count / samples;
21+
}
22+
}
23+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
3+
namespace MonteCarloIntegration
4+
{
5+
class Program
6+
{
7+
static void Main(string[] args)
8+
{
9+
var monteCarlo = new MonteCarlo();
10+
System.Console.WriteLine("Running with 10,000,000 samples.");
11+
var piEstimate = monteCarlo.Run(10000000);
12+
System.Console.WriteLine($"The estimate of pi is: {piEstimate}");
13+
System.Console.WriteLine($"The percent error is: {Math.Abs(piEstimate - Math.PI) / Math.PI * 100}%");
14+
}
15+
}
16+
}

contents/monte_carlo_integration/monte_carlo_integration.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ each point is tested to see whether it's in the circle or not:
6161
[import:15-17, lang:"swift"](code/swift/monte_carlo.swift)
6262
{% sample lang="py" %}
6363
[import:5-7, lang:"python"](code/python/monte_carlo.py)
64+
{% sample lang="cs" %}
65+
[import:23-23, lang:"csharp"](code/csharp/Circle.cs)
6466
{% endmethod %}
6567

6668
If it's in the circle, we increase an internal count by one, and in the end,
@@ -116,6 +118,13 @@ Feel free to submit your version via pull request, and thanks for reading!
116118
[import, lang:"swift"](code/swift/monte_carlo.swift)
117119
{% sample lang="py" %}
118120
[import, lang:"python"](code/python/monte_carlo.py)
121+
{% sample lang="cs" %}
122+
MonteCarlo.cs
123+
[import, lang:"csharp"](code/csharp/MonteCarlo.cs)
124+
Circle.cs
125+
[import, lang:"csharp"](code/csharp/Circle.cs)
126+
Program.cs
127+
[import, lang:"csharp"](code/csharp/Program.cs)
119128
{% endmethod %}
120129

121130

0 commit comments

Comments
 (0)