Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
added csharp to md files, updated contributors
  • Loading branch information
v.shvetsov committed Jul 10, 2018
commit 74d54d09449fcf303c867834d68ea2399573b369
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
23 changes: 13 additions & 10 deletions chapters/monte_carlo/code/csharp/MonteCarlo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
// submitted by vitalii shvetsov (@sanstorik)
using System;
using System.Collections.Generic;
using System.Text;

Expand All @@ -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;
}
}
3 changes: 2 additions & 1 deletion chapters/monte_carlo/code/csharp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
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).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);
Expand Down
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