Accord.Math.Integration.MonteCarloIntegration.Integrate C# (CSharp) Method

Integrate() public static method

Computes the area under the integral for the given function, in the given integration interval, using a Monte Carlo integration algorithm.
public static Integrate ( double>.Func func, double a, double b, int samples ) : double
func double>.Func The unidimensional function whose integral should be computed.
a double The beginning of the integration interval.
b double The ending of the integration interval.
samples int The number of points that should be sampled.
return double
        public static double Integrate(Func<double, double> func, double a, double b, int samples)
        {
            double volume = (b - a);

            int count = 0;
            double sum = 0;

            var random = new Random(Generator.Random.Next());

            for (count = 0; count < samples; count++)
            {
                double u = random.Next() * (b - a) + a;

                double f = func(u);

                count++;
                sum += f;
            }

            double avg = sum / count;

            return volume * avg;
        }

Same methods

MonteCarloIntegration::Integrate ( double>.Func func, double a, double b ) : double
MonteCarloIntegration::Integrate ( double>.Func func, double a, double b, int samples ) : double