bigInt function

Generator<BigInt> bigInt({
  1. BigInt? min,
  2. BigInt? max,
  3. BigInt? shrinkInterval,
})

Generates a random BigInt value uniformly distributed in the range from min, inclusive, to max, exclusive.

If min is not provided, it defaults to -size, and if max is not provided, it defaults to size.

shrinkInterval decides how fast the values are shrunk. If not provided, it defaults to BigInt.one.

Implementation

Generator<BigInt> bigInt({BigInt? min, BigInt? max, BigInt? shrinkInterval}) => generator(
  generate: (random, size) {
    final actualMin = min ?? BigInt.from(-size);
    final actualMax = max ?? BigInt.from(size);
    assert(actualMax > actualMin, 'min must be less than max');
    if (actualMax == actualMin) {
      return actualMin;
    }

    final valueRange = actualMax - actualMin;

    var result = BigInt.zero;
    for (var i = 0; i < valueRange.bitLength; i++) {
      final nextResult = (result << 1) + (random.nextBool() ? BigInt.one : BigInt.zero);
      if (actualMin + nextResult >= actualMax) {
        break;
      }
      result = nextResult;
    }
    return actualMin + result;
  },
  shrink: (input) sync* {
    if (input > BigInt.zero) {
      yield input - (shrinkInterval ?? BigInt.one);
    } else if (input < BigInt.zero) {
      yield input + (shrinkInterval ?? BigInt.one);
    }
  },
);