Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/randomized/MonteCarloIntegration.java (Java)

Code before:
 * <p>For a one-dimensional integral over [a, b], the approximation is the function range (b-a),
 * multiplied by the function average result for a random sample.
 * See more: <a href="https://en.wikipedia.org/wiki/Monte_Carlo_integration">Monte Carlo Integration</a>
 *
 * @author: MuhammadEzzatHBK
 */

public final class MonteCarloIntegration {

    private MonteCarloIntegration() {
    }

    /**
     * Approximates the definite integral of a given function over a specified
     * interval using the Monte Carlo method with a fixed random seed for
     * reproducibility.
     *
     * @param fx    the function to integrate
     * @param a     the lower bound of the interval
     * @param b     the upper bound of the interval
     * @param n     the number of random samples to use
     * @param seed  the seed for the random number generator
     * @return      the approximate value of the integral
     */
    public static double approximate(Function<Double, Double> fx, double a, double b, int n, long seed) {
        return doApproximate(fx, a, b, n, new Random(seed));
    }

    /**
     * Approximates the definite integral of a given function over a specified
     * interval using the Monte Carlo method with a random seed based on the
     * current system time for more randomness.
     *
     * @param fx    the function to integrate
     * @param a     the lower bound of the interval
     * @param b     the upper bound of the interval
     * @param n     the number of random samples to use
     * @return      the approximate value of the integral
     */
    public static double approximate(Function<Double, Double> fx, double a, double b, int n) {
        return doApproximate(fx, a, b, n, new Random(System.currentTimeMillis()));
    }

    private static double doApproximate(Function<Double, Double> fx, double a, double b, int n, Random generator) {
        double totalArea = 0.0;
        double range = b - a;
        for (int i = 0; i < n; i++) {
            double x = a + generator.nextDouble() * range;
            totalArea += fx.apply(x);
        }
        return range * totalArea / n;

Review comment (suggestion):
- Consider adding input validation (e.g., ensure a < b and n > 0) to make it more robust.
- Optionally, check if the function fx is null to prevent potential NullPointerExceptions.
- Great use of overloading to allow both seeded and unseeded randomness for reproducibility or variability😄

Code after:
 * <p>For a one-dimensional integral over [a, b], the approximation is the function range (b-a),
 * multiplied by the function average result for a random sample.
 * See more: <a href="https://en.wikipedia.org/wiki/Monte_Carlo_integration">Monte Carlo Integration</a>
 *
 * @author: MuhammadEzzatHBK
 */

public final class MonteCarloIntegration {

    private MonteCarloIntegration() {
    }

    /**
     * Approximates the definite integral of a given function over a specified
     * interval using the Monte Carlo method with a fixed random seed for
     * reproducibility.
     *
     * @param fx    the function to integrate
     * @param a     the lower bound of the interval
     * @param b     the upper bound of the interval
     * @param n     the number of random samples to use
     * @param seed  the seed for the random number generator
     * @return      the approximate value of the integral
     */
    public static double approximate(Function<Double, Double> fx, double a, double b, int n, long seed) {
        return doApproximate(fx, a, b, n, new Random(seed));
    }

    /**
     * Approximates the definite integral of a given function over a specified
     * interval using the Monte Carlo method with a random seed based on the
     * current system time for more randomness.
     *
     * @param fx    the function to integrate
     * @param a     the lower bound of the interval
     * @param b     the upper bound of the interval
     * @param n     the number of random samples to use
     * @return      the approximate value of the integral
     */
    public static double approximate(Function<Double, Double> fx, double a, double b, int n) {
        return doApproximate(fx, a, b, n, new Random(System.currentTimeMillis()));
    }

    private static double doApproximate(Function<Double, Double> fx, double a, double b, int n, Random generator) {
        if (!validate(fx, a, b, n)) {
            throw new IllegalArgumentException("Invalid input parameters");
        }
        double totalArea = 0.0;
        double interval = b - a;
        for (int i = 0; i < n; i++) {
            double x = a + generator.nextDouble() * interval;