TheAlgorithms/Java · error · IllegalArgumentException
Invalid input parameters
Error message
Invalid input parameters
What it means
Thrown by MonteCarloIntegration.approximate/doApproximate when validate() fails. validate() requires ALL of: fx != null, a < b (strict — a == b is invalid), and n > 0. The single generic message hides which condition failed, so you must check all three.
Source
Thrown at src/main/java/com/thealgorithms/randomized/MonteCarloIntegration.java:65
/**
* 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 total = 0.0;
double interval = b - a;
int pairs = n / 2;
for (int i = 0; i < pairs; i++) {
double u = generator.nextDouble();
double x1 = a + u * interval;
double x2 = a + (1.0 - u) * interval;
total += fx.apply(x1);
total += fx.apply(x2);
}
if ((n & 1) == 1) {
double x = a + generator.nextDouble() * interval;
total += fx.apply(x);
}
return interval * total / n;
}
View on GitHub (pinned to fdfb9a395b)
Solutions
- Ensure the integrand fx is a non-null Function<Double,Double> before calling.
- Pass bounds with a < b strictly; if your interval is [b, a] with b > a, swap them (or negate the result).
- Pass a positive sample count n (e.g. 1000 or more for usable accuracy).
Example fix
// before
MonteCarloIntegration.approximate(fx, upper, lower, 0); // bounds reversed, n=0
// after
double lo = Math.min(upper, lower);
double hi = Math.max(upper, lower);
double result = (fx != null && hi > lo && samples > 0)
? MonteCarloIntegration.approximate(fx, lo, hi, samples)
: Double.NaN; Defensive patterns
Strategy: validation
Validate before calling
if (fx == null || !(a < b) || n <= 0) {
throw new IllegalArgumentException("fx must be non-null, a < b, n > 0");
}
double approx = MonteCarloIntegration.approximate(fx, a, b, n); Type guard
static boolean validMonteCarlo(Function<Double,Double> fx, double a, double b, int n) {
return fx != null && a < b && n > 0;
} Try / catch
try {
double approx = MonteCarloIntegration.approximate(fx, a, b, n);
} catch (IllegalArgumentException e) {
// message is generic; re-check fx/a/b/n yourself to report the real cause
logger.warn("Monte Carlo rejected params: fx={}, a={}, b={}, n={}", fx != null, a, b, n);
} Prevention
- The exception message gives no detail — validate all three conditions yourself for a clear error.
- Always pass a < b strictly; swap bounds (and negate the result) for descending intervals.
- Use a non-trivial sample count (1000+) for usable accuracy; never default n to 0.
When it happens
Trigger: approximate(null, 0, 1, 100) (null function); approximate(f, 2, 1, 100) (a >= b); approximate(f, 0, 1, 0) (n <= 0); approximate(f, 0, 0, 10) (degenerate zero-width interval).
Common situations: Passing bounds in the wrong order (upper as lower); defaulting sample count to 0 in a config; a null Function reference when the integrand wasn't set; flipping a/b when integrating a descending interval.
Related errors
- Sample size cannot exceed stream size.
- Input '" + input + "' contains not only digits
- Focal length and object distance must be non-zero.
- Object distance must be non-zero.
- Input array cannot be null or empty.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/617b2b100f93b7c9.
Report an issue: GitHub.