Skip to content

Instantly share code, notes, and snippets.

@Ang3lFir3
Created March 22, 2011 23:59
Show Gist options
  • Save Ang3lFir3/882362 to your computer and use it in GitHub Desktop.
Save Ang3lFir3/882362 to your computer and use it in GitHub Desktop.
Monte Carlo Pi
using System;
public class Foo
{
public static void Main()
{
int insideCounter = 0;
var iteration = 60000000;
var randomiser = new Random((int)DateTime.Now.Ticks);
for(var i = 0; i < iteration; i++)
{
var x = randomiser.NextDouble();
var y = randomiser.NextDouble();
if(Math.Sqrt(Math.Pow(x,2) + Math.Pow(y,2)) <= 1.0d)
{
insideCounter++;
}
}
Console.WriteLine("{0} Hits were inside the area of the circle.", insideCounter);
Console.WriteLine(4d * insideCounter / iteration);
Console.ReadLine();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment