Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Saturday, December 13, 2008

Izračunavanje broja Pi


const int throws = 10000000;
DateTime now = DateTime.Now;
Random rand = new Random ((int) now.Millisecond);
int Inside = 0;
for (int i = 0; i < throws; ++i)
{
double cx = rand.NextDouble();
double cy = rand.NextDouble();
double distance = Math.Sqrt ((cx * cx) + (cy * cy));
if (distance < 1.0)
++Inside;
}
double pi = 4 * (double) Inside / (double) throws;
DateTime End = DateTime.Now;
TimeSpan Diff = End - now;
Console.WriteLine ("pi = " + pi);
Console.WriteLine ("Milliseconds = " + Diff.TotalMilliseconds);
Console.ReadLine();
http://feeds.feedburner.com/alisteroz