diff --git a/contents/monte_carlo_integration/code/csharp/Circle.cs b/contents/monte_carlo_integration/code/csharp/Circle.cs new file mode 100644 index 000000000..6464174c9 --- /dev/null +++ b/contents/monte_carlo_integration/code/csharp/Circle.cs @@ -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); + } +} diff --git a/contents/monte_carlo_integration/code/csharp/MonteCarlo.cs b/contents/monte_carlo_integration/code/csharp/MonteCarlo.cs new file mode 100644 index 000000000..36c73c186 --- /dev/null +++ b/contents/monte_carlo_integration/code/csharp/MonteCarlo.cs @@ -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; + } + } +} diff --git a/contents/monte_carlo_integration/code/csharp/Program.cs b/contents/monte_carlo_integration/code/csharp/Program.cs new file mode 100644 index 000000000..f29598a64 --- /dev/null +++ b/contents/monte_carlo_integration/code/csharp/Program.cs @@ -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}%"); + } + } +} diff --git a/contents/monte_carlo_integration/monte_carlo_integration.md b/contents/monte_carlo_integration/monte_carlo_integration.md index c9613f577..5b33a39a1 100644 --- a/contents/monte_carlo_integration/monte_carlo_integration.md +++ b/contents/monte_carlo_integration/monte_carlo_integration.md @@ -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, @@ -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 %}