TheAlgorithms/Java · error · RuntimeException

Number must be zero for random assignment!

Error message

Number must be zero for random assignment!

What it means

Thrown by Builder.rand(long seed) when the builder's internal number field is not 0. The MathBuilder design distinguishes accumulating operations (add, multiply) from assignment operations (rand, pi, e, set). Assignment operations require a clean slate (number == 0) to avoid ambiguity about which value wins. This is a RuntimeException, not an IllegalArgumentException.

Source

Thrown at src/main/java/com/thealgorithms/maths/MathBuilder.java:110

        }

        // Takes a number and a condition, only does the operation if condition is true.
        public Builder minusIf(double num, BiFunction<Double, Double, Boolean> condition) {
            if (!condition.apply(number, num)) {
                return this;
            }
            if (inParenthesis) {
                sideNumber -= num;
            } else {
                number -= num;
            }
            return this;
        }

        // Generates a random number and sets to NUMBER
        public Builder rand(long seed) {
            if (number != 0) {
                throw new RuntimeException("Number must be zero for random assignment!");
            }
            Random random = new Random();
            number = random.nextDouble(seed);
            return this;
        }

        // Takes PI value and sets to NUMBER
        public Builder pi() {
            if (number != 0) {
                throw new RuntimeException("Number must be zero for PI assignment!");
            }
            number = Math.PI;
            return this;
        }

        // Takes E value and sets to NUMBER
        public Builder e() {
            if (number != 0) {

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Call rand() on a fresh Builder constructed with no-arg new Builder() (number defaults to 0)
  2. If you need randomness after other operations, build the current state first, then start a new Builder
  3. Replace the assignment with an accumulating operation if you want to combine values

Example fix

// before
MathBuilder.Builder b = new MathBuilder.Builder(10).rand(42);

// after
MathBuilder.Builder b = new MathBuilder.Builder().rand(42);
Defensive patterns

Strategy: validation

Validate before calling

// Ensure a fresh builder before calling assignment methods
MathBuilder.Builder b = new MathBuilder.Builder();  // number defaults to 0
b.rand(42);

Try / catch

try {
    builder.rand(seed);
} catch (RuntimeException e) {
    // number was non-zero — reset and retry or create a new builder
    builder = new MathBuilder.Builder();
    builder.rand(seed);
}

Prevention

When it happens

Trigger: Calling rand(seed) after any operation that set number to non-zero: new MathBuilder.Builder(5).rand(42), or new MathBuilder.Builder().pi().rand(42), or after add/multiply that changed number from 0.

Common situations: Chaining builder calls fluently without understanding the assignment-vs-accumulation distinction. Starting from the Builder(double num) constructor with a non-zero value and then calling rand.

Related errors


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