var amount = 0.0d; var hitTheTargetCount = 0.0d; var M = 2.0d; var rnd=new Random(); for (int i = 0; i < 1000; i++) { var x = rnd.NextDouble() * Math.PI; var y = rnd.NextDouble() * M; //Console.WriteLine("x:{0},y:{1}", x, y); if (y <= Math.Sin(x)) { hitTheTargetCount++; } amount++; } var s = (hitTheTargetCount / amount) * M * Math.PI; Console.WriteLine(s);
相关文章: