bchavez/Bogus · error · ArgumentException

max digit can't be lager than 9 or smaller than 0

Error message

max digit can't be lager than 9 or smaller than 0

What it means

Thrown by Randomizer.Digits when maxDigit (or minDigit) is outside 0-9. The pattern 'maxDigit is > 9 or < 0' uses C# pattern matching; any value above 9 or below 0 is rejected. The same check runs for minDigit on the next line with a min-specific message.

Source

Thrown at Source/Bogus/Randomizer.cs:66

   /// <summary>
   /// Get an int from 0 to max.
   /// </summary>
   /// <param name="max">Upper bound, inclusive.</param>
   public int Number(int max)
   {
      return Number(0, max);
   }

   /// <summary>
   /// Get a random sequence of digits.
   /// </summary>
   /// <param name="count">How many</param>
   /// <param name="minDigit">minimum digit, inclusive</param>
   /// <param name="maxDigit">maximum digit, inclusive</param>
   public int[] Digits(int count, int minDigit = 0, int maxDigit = 9)
   {
      if( maxDigit is > 9 or < 0 ) throw new ArgumentException("max digit can't be lager than 9 or smaller than 0", nameof(maxDigit));
      if( minDigit is > 9 or < 0 ) throw new ArgumentException("min digit can't be lager than 9 or smaller than 0", nameof(minDigit));

      var digits = new int[count];
      for( var i = 0; i < count; i++ )
      {
         digits[i] = Number(min: minDigit, max: maxDigit);
      }
      return digits;
   }

   /// <summary>
   /// Get an int from min to max.
   /// </summary>
   /// <param name="min">Lower bound, inclusive</param>
   /// <param name="max">Upper bound, inclusive</param>
   public int Number(int min = 0, int max = 1)
   {
      //lock any seed access, for thread safety.

View on GitHub (pinned to 6ece18c5c2)

Solutions

  1. Clamp maxDigit and minDigit to the inclusive range 0..9 before calling Digits.
  2. Validate config-supplied bounds against 0..9 at load time.
  3. If you need values beyond 9, use Number(min,max) per position instead of Digits.

Example fix

// before
var d = f.Random.Digits(4, minDigit: 0, maxDigit: 15);
// after
var d = f.Random.Digits(4, minDigit: 0, maxDigit: 9);
Defensive patterns

Strategy: validation

Validate before calling

static int[] SafeDigits(Bogus.Randomizer r, int count, int minDigit, int maxDigit) {
    if (minDigit < 0 || minDigit > 9) throw new ArgumentOutOfRangeException(nameof(minDigit));
    if (maxDigit < 0 || maxDigit > 9) throw new ArgumentOutOfRangeException(nameof(maxDigit));
    return r.Digits(count, minDigit, maxDigit);
}

Try / catch

try { return r.Digits(count, minDigit, maxDigit); }
catch (ArgumentException ex) when (ex.Message.Contains("digit")) {
    // clamp bounds and retry
    return r.Digits(count, Math.Clamp(minDigit,0,9), Math.Clamp(maxDigit,0,9));
}

Prevention

When it happens

Trigger: Calling f.Random.Digits(5, 0, 12) or f.Random.Digits(5, -1, 9). Passing a digit bound outside the single-digit range.

Common situations: Configuring a digit generator from external config without clamping, or assuming the bounds are base-N rather than base-10 digit range.

Related errors


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