bchavez/Bogus · error · ArgumentException

The specified range does not contain any odd numbers.

Error message

The specified range does not contain any odd numbers.

What it means

Thrown by Randomizer.Odd when the range contains no odd numbers. Condition ((max & 1) == 0) && (min + 1 > max) means max is even and the range is too narrow to include an odd (e.g. a single even number).

Source

Thrown at Source/Bogus/Randomizer.cs:152

         return min;

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

   /// <summary>
   /// Returns a random odd number. If the range does not contain any odd 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 Odd(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( ((max & 1) == 0) && (min + 1 > max) )
         throw new ArgumentException("The specified range does not contain any odd numbers.", nameof(max));

      // Special case where the math below breaks.
      if ( max == int.MinValue )
         return int.MinValue | 1;

      // 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 = 2, max = 4, 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;
      max = (max - 1) | 1;

      if( min > max )
         return min | 1;

      // Ensure that the last bit is set in a random number to make the number odd.
      return Number(min, max) | 1;
   }

View on GitHub (pinned to 6ece18c5c2)

Solutions

  1. Widen the range so at least one odd number is included.
  2. Pre-validate: ensure some odd exists (min & 1) == 1 || max > min.
  3. Use Number() or Even() if odd is not strictly required.

Example fix

// before
var n = f.Random.Odd(2, 2);
// after
var n = f.Random.Odd(1, 3);
Defensive patterns

Strategy: validation

Validate before calling

static int SafeOdd(Bogus.Randomizer r, int min, int max) {
    bool hasOdd = ((max & 1) == 1) || (min + 1 <= max);
    if (!hasOdd) throw new InvalidOperationException($"range {min}..{max} has no odd number");
    return r.Odd(min, max);
}

Type guard

bool RangeHasOdd(int min, int max) => ((max & 1) == 1) || (max > min);

Try / catch

try { return r.Odd(min, max); }
catch (ArgumentException ex) when (ex.Message.Contains("does not contain any odd")) {
    return r.Odd(min, max + 1);
}

Prevention

When it happens

Trigger: Calling f.Random.Odd(min: 2, max: 2) - single even value, or Odd(4,4).

Common situations: Degenerate single-value ranges where the lone value is even; dynamically narrowed ranges.

Related errors


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