MathAbsoluteRandom
Math.abs does not always give a positive result. Please consider other methods for positive random numbers.

Severity
WARNING
Has Fix?
NO_FIX

The problem

Math.abs returns a negative number when called with the largest negative number.

Example:

int veryNegative = Math.abs(Integer.MIN_VALUE);
long veryNegativeLong = Math.abs(Long.MIN_VALUE);

When trying to generate positive random numbers by using Math.abs around a random positive-or-negative number, there will be (very infrequent) occasions where the random number will be negative.

Instead, one should use random number generation functions that are guaranteed to generate positive numbers:

Random r = new Random();
int positiveNumber = r.nextInt(Integer.MAX_VALUE);

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("MathAbsoluteRandom") to the enclosing element.