Repository: TheAlgorithms/Java
Pull request #6235 (2025-05-07 21:27:30)

Adding Monte Carlo's Integral Approximation
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6235?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 73.89%. Comparing base [(`571d05c`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/571d05caa8b851461d490cdfbba9124ee7af598a?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`b8b5b29`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/b8b5b294d5be13fc29fa4324d101d6020b1356b2?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #6235      +/-   ##
============================================
+ Coverage     73.88%   73.89%   +0.01%     
- Complexity     5314     5318       +4     
============================================
  Files           674      675       +1     
  Lines         18438    18446       +8     
  Branches       3568     3569       +1     
============================================
+ Hits          13623    13631       +8     
  Misses         4265     4265              
  Partials        550      550              
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6235?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

<details><summary> :rocket: New features to boost your workflow: </summary>

- :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
</details>

Patch:
diff --git a/src/main/java/com/thealgorithms/randomized/MonteCarloIntegration.java b/src/main/java/com/thealgorithms/randomized/MonteCarloIntegration.java
new file mode 100644
index 00000000..05d7abbb
--- /dev/null
+++ b/src/main/java/com/thealgorithms/randomized/MonteCarloIntegration.java
@@ -0,0 +1,82 @@
+package com.thealgorithms.randomized;
+
+import java.util.Random;
+import java.util.function.Function;
+
+/**
+ * A demonstration of the Monte Carlo integration algorithm in Java.
+ *
+ * <p>This class estimates the value of definite integrals using randomized sampling,
+ * also known as the Monte Carlo method. It is particularly effective for:
+ * <ul>
+ *   <li>Functions that are difficult or impossible to integrate analytically</li>
+ *   <li>High-dimensional integrals where traditional methods are inefficient</li>
+ *   <li>Simulation and probabilistic analysis tasks</li>
+ * </ul>
+ *
+ * <p>The core idea is to sample random points uniformly from the integration domain,
+ * evaluate the function at those points, and compute the scaled average to estimate the integral.
+ *
+ * <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;
+            totalArea += fx.apply(x);
+        }
+        return interval * totalArea / n;
+    }
+
+    private static boolean validate(Function<Double, Double> fx, double a, double b, int n) {
+        boolean isFunctionValid = fx != null;
+        boolean isIntervalValid = a < b;
+        boolean isSampleSizeValid = n > 0;
+        return isFunctionValid && isIntervalValid && isSampleSizeValid;
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/randomized/MonteCarloIntegrationTest.java b/src/test/java/com/thealgorithms/randomized/MonteCarloIntegrationTest.java
new file mode 100644
index 00000000..2a3a84b5
--- /dev/null
+++ b/src/test/java/com/thealgorithms/randomized/MonteCarloIntegrationTest.java
@@ -0,0 +1,91 @@
+package com.thealgorithms.randomized;
+
+import static com.thealgorithms.randomized.MonteCarloIntegration.approximate;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.util.function.Function;
+import org.junit.jupiter.api.Test;
+
+class MonteCarloIntegrationTest {
+
+    private static final double EPSILON = 0.03; // Allow 3% error margin
+
+    @Test
+    void testConstantFunction() {
+        // Integral of f(x) = 2 from 0 to 1 is 2
+        Function<Double, Double> constant = x -> 2.0;
+        double result = approximate(constant, 0, 1, 10000);
+        assertEquals(2.0, result, EPSILON);
+    }
+
+    @Test
+    void testLinearFunction() {
+        // Integral of f(x) = x from 0 to 1 is 0.5
+        Function<Double, Double> linear = Function.identity();
+        double result = approximate(linear, 0, 1, 10000);
+        assertEquals(0.5, result, EPSILON);
+    }
+
+    @Test
+    void testQuadraticFunction() {
+        // Integral of f(x) = x^2 from 0 to 1 is 1/3
+        Function<Double, Double> quadratic = x -> x * x;
+        double result = approximate(quadratic, 0, 1, 10000);
+        assertEquals(1.0 / 3.0, result, EPSILON);
+    }
+
+    @Test
+    void testLargeSampleSize() {
+        // Integral of f(x) = x^2 from 0 to 1 is 1/3
+        Function<Double, Double> quadratic = x -> x * x;
+        double result = approximate(quadratic, 0, 1, 50000000);
+        assertEquals(1.0 / 3.0, result, EPSILON / 2); // Larger sample size, smaller error margin
+    }
+
+    @Test
+    void testReproducibility() {
+        Function<Double, Double> linear = Function.identity();
+        double result1 = approximate(linear, 0, 1, 10000, 42L);
+        double result2 = approximate(linear, 0, 1, 10000, 42L);
+        assertEquals(result1, result2, 0.0); // Exactly equal
+    }
+
+    @Test
+    void testNegativeInterval() {
+        // Integral of f(x) = x from -1 to 1 is 0
+        Function<Double, Double> linear = Function.identity();
+        double result = approximate(linear, -1, 1, 10000);
+        assertEquals(0.0, result, EPSILON);
+    }
+
+    @Test
+    void testNullFunction() {
+        Exception exception = assertThrows(IllegalArgumentException.class, () -> approximate(null, 0, 1, 1000));
+        assertNotNull(exception);
+    }
+
+    @Test
+    void testInvalidInterval() {
+        Function<Double, Double> linear = Function.identity();
+        Exception exception = assertThrows(IllegalArgumentException.class, () -> {
+            approximate(linear, 2, 1, 1000); // b <= a
+        });
+        assertNotNull(exception);
+    }
+
+    @Test
+    void testZeroSampleSize() {
+        Function<Double, Double> linear = Function.identity();
+        Exception exception = assertThrows(IllegalArgumentException.class, () -> approximate(linear, 0, 1, 0));
+        assertNotNull(exception);
+    }
+
+    @Test
+    void testNegativeSampleSize() {
+        Function<Double, Double> linear = Function.identity();
+        Exception exception = assertThrows(IllegalArgumentException.class, () -> approximate(linear, 0, 1, -100));
+        assertNotNull(exception);
+    }
+}