{
public static final int COUNT=100000000;
public static void main(String[] args)
{
long start=System.currentTimeMillis();
Random random=new Random(start);
int inside=0;
for(int i=0;i<COUNT;i++)
{
double cx=random.nextDouble();
double cy=random.nextDouble();
double distance=Math.sqrt((cx*cx)+(cy*cy));
if(distance<1.0)
{
++inside;
}
}
double pi=4*(double)inside/(double)COUNT;
long end=System.currentTimeMillis();
long lastTime=end-start;
System.out.println ("pi="+pi);
System.out.println ("所用时间:"+lastTime+"ms");
}
}
{
public const int Count=100000000;
public static void Main(string[] args)
{
DateTime start=DateTime.Now;
Random random=new Random(start.Millisecond);
int Inside=0;
for(int i=0;i<Count;i++)
{
double cx=random.NextDouble();
double cy=random.NextDouble();
double distance=Math.Sqrt((cx*cx)+(cy*cy));
if(distance<1.0)
{
++Inside;
}
}
double pi=4*(double)Inside/(double)Count;
DateTime end=DateTime.Now;
TimeSpan diff=end-start;
Console.WriteLine("pi={0}",pi);
Console.WriteLine("消耗时间:{0}ms",diff.TotalMilliseconds);
}
}