-
-
Notifications
You must be signed in to change notification settings - Fork 360
added monte carlo method c# #239
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,3 +40,4 @@ GuyPozner | |
<br> | ||
William Boyles | ||
<br> | ||
sanstorik |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// submitted by vitalii shvetsov (@sanstorik) | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace MonteCarlo | ||
{ | ||
class MonteCarlo | ||
{ | ||
private static Random random = new Random(47); | ||
private int Radius { get; set; } | ||
private int Samples { get; set; } | ||
|
||
|
||
public MonteCarlo(int radius = 1, int samples = 1000000) | ||
{ | ||
Radius = radius; | ||
Samples = samples; | ||
} | ||
|
||
|
||
public double Run() | ||
{ | ||
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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
|
||
// submitted by vitalii shvetsov (@sanstorik) | ||
namespace MonteCarlo | ||
{ | ||
class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
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); | ||
|
||
Console.ReadLine(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
||
$$ | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to put |
||
[import, lang:"csharp"](code/csharp/MonteCarlo.cs) | ||
{% endmethod %} | ||
|
||
|
||
|
There was a problem hiding this comment.
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?