var amount = 0.0d;
var hitTheTargetCount = 0.0d;
var M = 2.0d;
var rnd=new Random();
for (int i = ; i < ; 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);
以上代码针对 sin(x) 0-pi 进行积分
积分int( sqrt(1-x^2),0,1)
private void button1_Click(object sender, EventArgs e)
{
var amount = 0.0d;
var hitTheTargetCount = 0.0d;
var M = 1.0d;
var rnd=new Random();
for (int i = ; i < ; i++)
{
var x = rnd.NextDouble() * M;
var y = rnd.NextDouble() * M;
//Console.WriteLine("x:{0},y:{1}", x, y);
if (y <= Math.Sqrt(-Math.Pow(x,)))
{
hitTheTargetCount++;
}
amount++;
} var s = (hitTheTargetCount / amount) * M * M;
Console.WriteLine(s * );
} private void button2_Click(object sender, EventArgs e)
{
Func<double, double> func = (x) => { return (Math.Sqrt( - Math.Pow(x, ))); }; var n = ;
double h = 1.0d / n;
double sumx = ;
double sum = func(0.0); for (int i = ; i < n; i++)
{
sumx += h;
var factor = ((i % ) == ) ? : ;
//Console.WriteLine(i +":" + factor +"," + sumx);
sum += factor * func(sumx);
}
sum += func();
var v = * (sum * h) / ;
Console.WriteLine(v);
}