{"record":{"id":"61fe3722778544bb","repo":"TheAlgorithms/Java","slug":"persistence-must-be-in-0-1","errorCode":null,"errorMessage":"persistence must be in (0, 1]","messagePattern":"persistence must be in \\(0, 1\\]","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/others/PerlinNoise.java","lineNumber":62,"sourceCode":"     *\n     * @param width       width of the noise array (columns)\n     * @param height      height of the noise array (rows)\n     * @param octaveCount number of octaves (layers) to blend; must be >= 1\n     * @param persistence per-octave amplitude multiplier in (0, 1]\n     * @param seed        seed for the random base grid\n     * @return a {@code width x height} array containing blended noise values in [0,\n     *         1]\n     */\n    static float[][] generatePerlinNoise(int width, int height, int octaveCount, float persistence, long seed) {\n        if (width <= 0 || height <= 0) {\n            throw new IllegalArgumentException(\"width and height must be > 0\");\n        }\n\n        if (octaveCount < 1) {\n            throw new IllegalArgumentException(\"octaveCount must be >= 1\");\n        }\n        if (!(persistence > 0f && persistence <= 1f)) { // using > to exclude 0 and NaN\n            throw new IllegalArgumentException(\"persistence must be in (0, 1]\");\n        }\n        final float[][] base = createBaseGrid(width, height, seed);\n        final float[][][] layers = createLayers(base, width, height, octaveCount);\n        return blendAndNormalize(layers, width, height, persistence);\n    }\n\n    /** Create the base random lattice values in [0,1). */\n    static float[][] createBaseGrid(int width, int height, long seed) {\n        final float[][] base = new float[width][height];\n        Random random = new Random(seed);\n        for (int x = 0; x < width; x++) {\n            for (int y = 0; y < height; y++) {\n                base[x][y] = random.nextFloat();\n            }\n        }\n        return base;\n    }\n","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/others/PerlinNoise.java#L44-L80","documentation":"Thrown by generatePerlinNoise when the persistence parameter falls outside the open-closed interval (0, 1]. Persistence is the per-octave amplitude multiplier used when blending noise layers; values outside (0, 1] break the geometric amplitude series (a persistence of 0 zeroes every layer, and >1 unbounds the amplitude sum). The check uses !(persistence > 0f && persistence <= 1f) specifically to reject NaN as well, since NaN comparisons always evaluate false.","triggerScenarios":"Calling generatePerlinNoise(width, height, octaveCount, persistence, seed) with persistence == 0f, persistence > 1f, or persistence == Float.NaN. Also triggered by accidentally passing an integer 0 or a negative value where a float persistence is expected.","commonSituations":"Supplying 0 because the field was uninitialized, defaulting persistence to 0 in a config object, passing a percentage like 50 (meaning 50%) instead of 0.5, or propagating Float.NaN from a prior computation that divided by zero or parsed a bad value.","solutions":["Pass a persistence strictly greater than 0 and at most 1; typical terrain/noise values are 0.5 to 0.8.","If persistence is derived from user input or config, clamp and validate it in range before calling generatePerlinNoise.","Guard against NaN explicitly: check !Float.isNaN(persistence) before the call if the value can originate from untrusted arithmetic.","If you intended a flat (zero-blend) noise field, that is not representable here — use a single octave with persistence 1 instead."],"exampleFix":"// before\nfloat[][] n = PerlinNoise.generatePerlinNoise(256, 256, 5, 0f, 42L);\n// after\nfloat[][] n = PerlinNoise.generatePerlinNoise(256, 256, 5, 0.5f, 42L);","handlingStrategy":"validation","validationCode":"private static void validatePersistence(float p) {\n    if (Float.isNaN(p) || !(p > 0f && p <= 1f)) {\n        throw new IllegalArgumentException(\"persistence must be in (0, 1], got \" + p);\n    }\n}\n// call before generatePerlinNoise","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat persistence as a configuration constant (e.g. 0.5f) sourced from a validated config object, not free user input.","When persistence is computed, guard both range and NaN at the point of computation.","Write a unit test asserting persistence 0 and persistence 1.01 each throw."],"tags":["java","parameter-validation","procedural-generation","noise","illegal-argument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}