getInteger function

int getInteger(
  1. int digitCount
)

Generates a random positive integer with exactly digitCount digits.

The first digit is always non-zero to ensure the correct number of digits.

digitCount must be between 1 and 17, inclusive. Throws a RangeError if the value is out of bounds.

Example:

int number = getInteger(5); // e.g., 34829

Returns an integer with the specified number of digits.

Implementation

int getInteger(int digitCount) {
  if (digitCount > _maxNumericDigits || digitCount < 1) {
    throw RangeError.range(0, 1, _maxNumericDigits, "Digit Count");
  }
  var digit = _random.nextInt(9) + 1; // first digit must not be a zero
  int n = digit;

  for (var i = 0; i < digitCount - 1; i++) {
    digit = _random.nextInt(10);
    n *= 10;
    n += digit;
  }
  return n;
}