{"record":{"id":"18bc4c24432951ca","repo":"TheAlgorithms/Java","slug":"probability-should-be-from-0-to-1-but-was-prob","errorCode":null,"errorMessage":"Probability should be from 0 to 1. But was: ${probability}","messagePattern":"Probability should be from 0 to 1\\. But was: (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/lists/SkipList.java","lineNumber":303,"sourceCode":"     * <p>\n     * Maximum height that would give the best search complexity\n     * calculated by <code>log<sub>1/p</sub>n</code>\n     * where {@code n} is an expected count of elements in list.\n     */\n    public static class BernoulliHeightStrategy implements HeightStrategy {\n\n        private final double probability;\n\n        private static final double DEFAULT_PROBABILITY = 0.5;\n        private static final Random RANDOM = new Random();\n\n        public BernoulliHeightStrategy() {\n            this.probability = DEFAULT_PROBABILITY;\n        }\n\n        public BernoulliHeightStrategy(double probability) {\n            if (probability <= 0 || probability >= 1) {\n                throw new IllegalArgumentException(\"Probability should be from 0 to 1. But was: \" + probability);\n            }\n            this.probability = probability;\n        }\n\n        @Override\n        public int height(int expectedSize) {\n            long height = Math.round(Math.log10(expectedSize) / Math.log10(1 / probability));\n            if (height > Integer.MAX_VALUE) {\n                throw new IllegalArgumentException();\n            }\n            return (int) height;\n        }\n\n        @Override\n        public int nodeHeight(int heightCap) {\n            int level = 0;\n            double border = 100 * (1 - probability);\n            while (((RANDOM.nextInt(Integer.MAX_VALUE) % 100) + 1) > border) {","sourceCodeStart":285,"sourceCodeEnd":321,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/lists/SkipList.java#L285-L321","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nnew BernoulliHeightStrategy(prob); // prob may be 0 or 1\n\n// after\nif (prob <= 0 || prob >= 1) {\n    prob = 0.5; // sane default\n}\nnew BernoulliHeightStrategy(prob);","handlingStrategy":"validation","validationCode":"if (probability > 0 && probability < 1) {\n    new BernoulliHeightStrategy(probability);\n} else {\n    new BernoulliHeightStrategy(); // default 0.5\n}","typeGuard":null,"tryCatchPattern":"try {\n    new BernoulliHeightStrategy(probability);\n} catch (IllegalArgumentException e) {\n    new BernoulliHeightStrategy();\n}","preventionTips":["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."],"tags":["skip-list","invalid-argument","data-structure","config","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}