TheAlgorithms/Java · error · IllegalArgumentException
Probability should be from 0 to 1. But was: ${probability}
Error message
Probability should be from 0 to 1. But was: ${probability} What it means
Thrown by SkipList.BernoulliHeightStrategy(double probability) when probability is <= 0 or >= 1. The strategy uses probability to compute the geometric height formula `log(expectedSize)/log(1/probability)`, which divides by zero or is undefined at the boundaries. The library requires an open interval (0, 1).
Source
Thrown at src/main/java/com/thealgorithms/datastructures/lists/SkipList.java:303
* <p>
* Maximum height that would give the best search complexity
* calculated by <code>log<sub>1/p</sub>n</code>
* where {@code n} is an expected count of elements in list.
*/
public static class BernoulliHeightStrategy implements HeightStrategy {
private final double probability;
private static final double DEFAULT_PROBABILITY = 0.5;
private static final Random RANDOM = new Random();
public BernoulliHeightStrategy() {
this.probability = DEFAULT_PROBABILITY;
}
public BernoulliHeightStrategy(double probability) {
if (probability <= 0 || probability >= 1) {
throw new IllegalArgumentException("Probability should be from 0 to 1. But was: " + probability);
}
this.probability = probability;
}
@Override
public int height(int expectedSize) {
long height = Math.round(Math.log10(expectedSize) / Math.log10(1 / probability));
if (height > Integer.MAX_VALUE) {
throw new IllegalArgumentException();
}
return (int) height;
}
@Override
public int nodeHeight(int heightCap) {
int level = 0;
double border = 100 * (1 - probability);
while (((RANDOM.nextInt(Integer.MAX_VALUE) % 100) + 1) > border) {View on GitHub (pinned to fdfb9a395b)
Solutions
- Use the no-arg constructor BernoulliHeightStrategy() for the default 0.5.
- Validate probability is strictly between 0 and 1 before constructing.
- Clamp external config values into (0, 1) with a small epsilon.
- Audit any ratio that feeds probability to ensure it cannot reach 0 or 1.
Example fix
// before
new BernoulliHeightStrategy(prob); // prob may be 0 or 1
// after
if (prob <= 0 || prob >= 1) {
prob = 0.5; // sane default
}
new BernoulliHeightStrategy(prob); Defensive patterns
Strategy: validation
Validate before calling
if (probability > 0 && probability < 1) {
new BernoulliHeightStrategy(probability);
} else {
new BernoulliHeightStrategy(); // default 0.5
} Try / catch
try {
new BernoulliHeightStrategy(probability);
} catch (IllegalArgumentException e) {
new BernoulliHeightStrategy();
} Prevention
- Use the no-arg constructor for the safe 0.5 default.
- Clamp external config values into (0, 1) with an epsilon margin.
- Audit ratios that feed probability to ensure they stay strictly inside the open interval.
When it happens
Trigger: Constructing BernoulliHeightStrategy with 0.0 or 1.0. Loading probability from a config file where the value was set to a boundary. Computing probability from a ratio that can hit exactly 0 or 1.
Common situations: Config typos setting probability to 1 or 0. Probability derived from count/total where count == 0 or count == total. Defaulting to the constructor's 0.5 is safe, but explicit boundary values are rejected.
Related errors
- Size must be greater than 0
- Cannot extract from empty heap
- Cannot insert null element
- Cannot delete from empty heap
- MinPriorityQueue is full. Cannot insert new element.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/18bc4c24432951ca.
Report an issue: GitHub.