From 6e54bdace7e4c7869b1fdfd97e4c478ed339c045 Mon Sep 17 00:00:00 2001 From: "v.shvetsov" Date: Mon, 9 Jul 2018 16:06:34 +0300 Subject: [PATCH 1/3] added monte carlo method c# --- chapters/monte_carlo/code/c#/Program.cs | 46 +++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 chapters/monte_carlo/code/c#/Program.cs diff --git a/chapters/monte_carlo/code/c#/Program.cs b/chapters/monte_carlo/code/c#/Program.cs new file mode 100644 index 000000000..d79726a3b --- /dev/null +++ b/chapters/monte_carlo/code/c#/Program.cs @@ -0,0 +1,46 @@ +using System; + +namespace MonteCarlo +{ + class Program + { + private static Random random = new Random(47); + private const int radius = 1; + + + public static void Main(string[] args) + { + double piEstimate = MonteCarloMethod(1000000); + + Console.WriteLine("Pi estimate = {0}", piEstimate); + Console.WriteLine("Pi error = {0}", 100 * (Math.Abs(Math.PI - piEstimate)) / Math.PI); + + Console.ReadLine(); + } + + + private static double MonteCarloMethod(int samples) + { + 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 static bool IsInCircle(double x, double y) + { + return x * x + y * y < radius * radius; + } + } +} From 300dd6479e9405cd0614caa1508bf277af655fdd Mon Sep 17 00:00:00 2001 From: "v.shvetsov" Date: Tue, 10 Jul 2018 09:30:20 +0300 Subject: [PATCH 2/3] renamed directory to csharp. Moved algorithm to another file --- chapters/monte_carlo/code/c#/Program.cs | 46 ------------------- .../monte_carlo/code/csharp/MonteCarlo.cs | 37 +++++++++++++++ chapters/monte_carlo/code/csharp/Program.cs | 17 +++++++ 3 files changed, 54 insertions(+), 46 deletions(-) delete mode 100644 chapters/monte_carlo/code/c#/Program.cs create mode 100644 chapters/monte_carlo/code/csharp/MonteCarlo.cs create mode 100644 chapters/monte_carlo/code/csharp/Program.cs diff --git a/chapters/monte_carlo/code/c#/Program.cs b/chapters/monte_carlo/code/c#/Program.cs deleted file mode 100644 index d79726a3b..000000000 --- a/chapters/monte_carlo/code/c#/Program.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; - -namespace MonteCarlo -{ - class Program - { - private static Random random = new Random(47); - private const int radius = 1; - - - public static void Main(string[] args) - { - double piEstimate = MonteCarloMethod(1000000); - - Console.WriteLine("Pi estimate = {0}", piEstimate); - Console.WriteLine("Pi error = {0}", 100 * (Math.Abs(Math.PI - piEstimate)) / Math.PI); - - Console.ReadLine(); - } - - - private static double MonteCarloMethod(int samples) - { - 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 static bool IsInCircle(double x, double y) - { - return x * x + y * y < radius * radius; - } - } -} diff --git a/chapters/monte_carlo/code/csharp/MonteCarlo.cs b/chapters/monte_carlo/code/csharp/MonteCarlo.cs new file mode 100644 index 000000000..bb5b21155 --- /dev/null +++ b/chapters/monte_carlo/code/csharp/MonteCarlo.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace MonteCarlo +{ + class MonteCarlo + { + private static Random random = new Random(47); + private readonly int radius = 1; + + + public MonteCarlo(int radius) + { + this.radius = radius; + } + + + public double Run(int samples) + { + 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; + } +} diff --git a/chapters/monte_carlo/code/csharp/Program.cs b/chapters/monte_carlo/code/csharp/Program.cs new file mode 100644 index 000000000..2298a6060 --- /dev/null +++ b/chapters/monte_carlo/code/csharp/Program.cs @@ -0,0 +1,17 @@ +using System; + +namespace MonteCarlo +{ + class Program + { + public static void Main(string[] args) + { + double piEstimate = new MonteCarlo(1).Run(1000000); + + Console.WriteLine("Pi estimate = {0}", piEstimate); + Console.WriteLine("Pi error = {0}", 100 * (Math.Abs(Math.PI - piEstimate)) / Math.PI); + + Console.ReadLine(); + } + } +} From 74d54d09449fcf303c867834d68ea2399573b369 Mon Sep 17 00:00:00 2001 From: "v.shvetsov" Date: Tue, 10 Jul 2018 13:17:38 +0300 Subject: [PATCH 3/3] added csharp to md files, updated contributors --- CONTRIBUTORS.md | 1 + .../monte_carlo/code/csharp/MonteCarlo.cs | 23 +++++++++++-------- chapters/monte_carlo/code/csharp/Program.cs | 3 ++- chapters/monte_carlo/monte_carlo.md | 5 ++++ 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 50199d0d4..0fffc591a 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -40,3 +40,4 @@ GuyPozner
William Boyles
+sanstorik diff --git a/chapters/monte_carlo/code/csharp/MonteCarlo.cs b/chapters/monte_carlo/code/csharp/MonteCarlo.cs index bb5b21155..390f95d5f 100644 --- a/chapters/monte_carlo/code/csharp/MonteCarlo.cs +++ b/chapters/monte_carlo/code/csharp/MonteCarlo.cs @@ -1,4 +1,5 @@ -using System; +// submitted by vitalii shvetsov (@sanstorik) +using System; using System.Collections.Generic; using System.Text; @@ -7,31 +8,33 @@ namespace MonteCarlo class MonteCarlo { private static Random random = new Random(47); - private readonly int radius = 1; + private int Radius { get; set; } + private int Samples { get; set; } - public MonteCarlo(int radius) + public MonteCarlo(int radius = 1, int samples = 1000000) { - this.radius = radius; + Radius = radius; + Samples = samples; } - public double Run(int samples) + public double Run() { int piCount = 0; - for (int i = 0; i < samples; i++) + for (int i = 0; i < Samples; i++) { - var randomX = random.NextDouble() * radius; - var randomY = random.NextDouble() * radius; + var randomX = random.NextDouble() * Radius; + var randomY = random.NextDouble() * Radius; if (IsInCircle(randomX, randomY)) piCount++; } - return 4 * piCount / (double)samples; + return 4 * piCount / (double)Samples; } - private bool IsInCircle(double x, double y) => x * x + y * y < radius * radius; + private bool IsInCircle(double x, double y) => x * x + y * y < Radius * Radius; } } diff --git a/chapters/monte_carlo/code/csharp/Program.cs b/chapters/monte_carlo/code/csharp/Program.cs index 2298a6060..576d3e3fd 100644 --- a/chapters/monte_carlo/code/csharp/Program.cs +++ b/chapters/monte_carlo/code/csharp/Program.cs @@ -1,12 +1,13 @@ using System; +// submitted by vitalii shvetsov (@sanstorik) namespace MonteCarlo { class Program { public static void Main(string[] args) { - double piEstimate = new MonteCarlo(1).Run(1000000); + 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); diff --git a/chapters/monte_carlo/monte_carlo.md b/chapters/monte_carlo/monte_carlo.md index e777bdaa4..6171afa18 100644 --- a/chapters/monte_carlo/monte_carlo.md +++ b/chapters/monte_carlo/monte_carlo.md @@ -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) {% endmethod %} + If it's in the circle, we increase an internal count by one, and in the end, $$ @@ -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 +[import, lang:"csharp"](code/csharp/MonteCarlo.cs) {% endmethod %}