TheAlgorithms/Java · error · IllegalArgumentException
persistence must be in (0, 1]
Error message
persistence must be in (0, 1]
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/others/PerlinNoise.java:62
*
* @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();
}
}
return base;
}
View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before float[][] n = PerlinNoise.generatePerlinNoise(256, 256, 5, 0f, 42L); // after float[][] n = PerlinNoise.generatePerlinNoise(256, 256, 5, 0.5f, 42L);
Defensive patterns
Strategy: validation
Validate before calling
private static void validatePersistence(float p) {
if (Float.isNaN(p) || !(p > 0f && p <= 1f)) {
throw new IllegalArgumentException("persistence must be in (0, 1], got " + p);
}
}
// call before generatePerlinNoise Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Orbiting mass and radius must be positive.
- Natural frequency must be positive.
- Damping coefficient must be non-negative.
- State must be a non-null array of length 2.
- Time step must be positive.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/61fe3722778544bb.
Report an issue: GitHub.