TheAlgorithms/Java · error · IllegalArgumentException

octaveCount must be >= 1

Error message

octaveCount must be >= 1

What it means

Thrown by PerlinNoise.generatePerlinNoise when octaveCount < 1. Octaves are the blended noise layers; at least one layer is required to produce any output. Zero or negative octaves would leave the layer array empty and produce no noise, so the method rejects it.

Source

Thrown at src/main/java/com/thealgorithms/others/PerlinNoise.java:59

    /**
     * Generate a 2D array of blended noise values normalized to [0, 1].
     *
     * @param width       width of the noise array (columns)
     * @param height      height of the noise array (rows)
     * @param octaveCount number of octaves (layers) to blend; must be >= 1
     * @param persistence per-octave amplitude multiplier in (0, 1]
     * @param seed        seed for the random base grid
     * @return a {@code width x height} array containing blended noise values in [0,
     *         1]
     */
    static float[][] generatePerlinNoise(int width, int height, int octaveCount, float persistence, long seed) {
        if (width <= 0 || height <= 0) {
            throw new IllegalArgumentException("width and height must be > 0");
        }

        if (octaveCount < 1) {
            throw new IllegalArgumentException("octaveCount must be >= 1");
        }
        if (!(persistence > 0f && persistence <= 1f)) { // using > to exclude 0 and NaN
            throw new IllegalArgumentException("persistence must be in (0, 1]");
        }
        final float[][] base = createBaseGrid(width, height, seed);
        final float[][][] layers = createLayers(base, width, height, octaveCount);
        return blendAndNormalize(layers, width, height, persistence);
    }

    /** Create the base random lattice values in [0,1). */
    static float[][] createBaseGrid(int width, int height, long seed) {
        final float[][] base = new float[width][height];
        Random random = new Random(seed);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                base[x][y] = random.nextFloat();
            }
        }

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Pass octaveCount >= 1 (4-8 is typical for natural-looking noise).
  2. Default octaveCount to a positive value when unset.
  3. Clamp detail-derived octave counts to a minimum of 1.
  4. Validate the parameter at the boundary where it is parsed.

Example fix

// before
float[][] noise = PerlinNoise.generatePerlinNoise(w, h, octaves, persistence, seed); // octaves may be 0

// after
int oct = Math.max(1, octaves);
float[][] noise = PerlinNoise.generatePerlinNoise(w, h, oct, persistence, seed);
Defensive patterns

Strategy: validation

Validate before calling

int octaves = Math.max(1, octaveCount);
float[][] noise = PerlinNoise.generatePerlinNoise(width, height, octaves, persistence, seed);

Type guard

public static boolean isPositiveOctaves(int octaves) {
    return octaves >= 1;
}

Try / catch

try {
    noise = PerlinNoise.generatePerlinNoise(w, h, oct, p, seed);
} catch (IllegalArgumentException e) {
    noise = PerlinNoise.generatePerlinNoise(w, h, 1, p, seed);
}

Prevention

When it happens

Trigger: Calling generatePerlinNoise(..., octaveCount, ...) with octaveCount of 0 or negative; a 'detail level' setting mapped to 0.

Common situations: A detail/quality slider that can reach 0; default value unset resolving to 0; logic that subtracts from octaveCount until it hits 0.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/3b4c619b168de47a. Report an issue: GitHub.