Skip to content

Add Monte Carlo Integration in C# #309

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

Merged
merged 4 commits into from
Aug 2, 2018
Merged

Add Monte Carlo Integration in C# #309

merged 4 commits into from
Aug 2, 2018

Conversation

june128
Copy link
Member

@june128 june128 commented Jul 28, 2018

Since this PR is dead, I wrote it myself.

  • UseMath.Pow instead of point.X * point.X.
  • Make the error a percentage.
  • Use 10,000,000 instead of 10.000.000 for the console output.
  • Calculate the percent error in Main.

System.Console.WriteLine($"The estimate of pi is: {output.PiEstimate}");
System.Console.WriteLine($"The error is: {output.Error}");

System.Console.WriteLine("How many samples should be used?");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I leave this custom user input in?


public Circle(double radius) => this.Radius = Math.Abs(radius);

public bool IsInMe(Point point) => point.X * point.X + point.Y * point.Y < Radius * Radius;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used point.X * point.X now instead of using Math.Pow. It doesn't really matter here, what do you find cleaner?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just leave Math.Pow ... it look then more compact and less chance of missreading

@Gathros Gathros added the Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) label Jul 28, 2018
}

var piEstimate = 4.0 * count / samples;
var error = Math.Abs(Math.PI - piEstimate);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make the error a percentage (and add % when you print out the error) to remove ambiguity

static void Main(string[] args)
{
var monteCarlo = new MonteCarlo();
System.Console.WriteLine("Running with 10.000.000 samples.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it should be 10,000,000?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, just thought german there :D

Copy link
Member

@PudottaPommin PudottaPommin Jul 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use 10 000 000 then or word notation 10 million

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think 10,000,000 is fine since the example is in english.

System.Console.WriteLine($"The estimate of pi is: {piEstimate.Estimate}");
System.Console.WriteLine($"The error is: {piEstimate.Error}");

System.Console.WriteLine("How many samples should be used?");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally would not include this input part's to code examples. I would keep them minimal.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally like to give the user a choice here, but I understand that it makes things quite a bit more messy.

@june128 june128 changed the title Add Monte Carlo Integration. Add Monte Carlo Integration in C# Aug 1, 2018
…adable. Use percent error. Remove user input.
}

var piEstimate = 4.0 * count / samples;
var percentError = (piEstimate - Math.PI) / Math.PI * 100;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last thing: it feels strange to me to use Math.PIinside of a function that estimates pi. I've usually recommended to other people to calculate the error in main.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I see what you mean. I'm gonna change that.

@jiegillet
Copy link
Member

Whoa, +4 -18 for moving a single line. OOP really is verbose :)

@june128 june128 merged commit 7caac1c into algorithm-archivists:master Aug 2, 2018
@june128
Copy link
Member Author

june128 commented Aug 3, 2018

@jiegillet I didn't need that output class anymore :D

@june128 june128 deleted the add_Monte_Carlo_C# branch August 3, 2018 00:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants