bchavez/Bogus · error · ArgumentException

The min/max range is invalid. The minimum value '{min}' is g

Error message

The min/max range is invalid. The minimum value '{min}' is greater than the maximum value '{max}'.

What it means

Thrown by Randomizer.Even when min > max. The check is the first validation in Even(); it guards the inclusive range before the parity logic. The exception param is named 'max'.

Source

Thrown at Source/Bogus/Randomizer.cs:122

         int topHalf = (sample1 >> 8) & 0xFFFF;
         int bottomHalf = (sample2 >> 8) & 0xFFFF;

         return unchecked((topHalf << 16) | bottomHalf);
      }
   }

   /// <summary>
   /// Returns a random even number. If the range does not contain any even numbers, an <see cref="ArgumentException" /> is thrown.
   /// </summary>
   /// <param name="min">Lower bound, inclusive</param>
   /// <param name="max">Upper bound, inclusive</param>
   /// <exception cref="ArgumentException">Thrown if it is impossible to select an odd number satisfying the specified range.</exception>
   public int Even(int min = 0, int max = 1)
   {
      // Ensure that we have a valid range.
      if( min > max )
         throw new ArgumentException($"The min/max range is invalid. The minimum value '{min}' is greater than the maximum value '{max}'.", nameof(max));
      if( ((min & 1) == 1) && (max - 1 < min) )
         throw new ArgumentException("The specified range does not contain any even numbers.", nameof(max));

      // Adjust the range to ensure that we always get the same number of even values as odd values.
      // For example,
      //   if the input is min = 1, max = 3, the new range should be min = 2, max = 3.
      //   if the input is min = 2, max = 3, the range should remain min = 2, max = 3.
      min = (min + 1) & ~1;
      max = max | 1;

      if( min > max )
         return min;

      // Strip off the last bit of a random number to make the number even.
      return Number(min, max) & ~1;
   }

   /// <summary>

View on GitHub (pinned to 6ece18c5c2)

Solutions

  1. Ensure min <= max before calling Even.
  2. Normalize bounds: if (min > max) (min, max) = (max, min);
  3. Validate input bounds at the source (config / user input) before reaching the randomizer.

Example fix

// before
var n = f.Random.Even(min: hi, max: lo);
// after
int lo = Math.Min(hi, lo0), hi = Math.Max(hi, lo0);
var n = f.Random.Even(min: lo, max: hi);
Defensive patterns

Strategy: validation

Validate before calling

static int SafeEven(Bogus.Randomizer r, int min, int max) {
    if (min > max) (min,max) = (max,min);
    return r.Even(min, max);
}

Try / catch

try { return r.Even(min, max); }
catch (ArgumentException ex) when (ex.Message.Contains("minimum value") && ex.Message.Contains("greater")) {
    return r.Even(Math.Min(min,max), Math.Max(min,max));
}

Prevention

When it happens

Trigger: Calling f.Random.Even(min: 10, max: 2) i.e. swapping bounds or passing a descending range.

Common situations: Computing min/max from external inputs where order is not enforced; off-by-one in bound calculation.

Related errors


AI-assisted analysis of bchavez/Bogus@6ece18c5c2 (2026-08-13). Data as JSON: /api/errors/5e3ca0730ed78f9a. Report an issue: GitHub.