bigIntInRange method

Generator<BigInt> bigIntInRange(
  1. BigInt? min,
  2. BigInt? max
)

A generator that returns BigInts between min, inclusive, to max, exclusive.

Implementation

Generator<core.BigInt> bigIntInRange(core.BigInt? min, core.BigInt? max) {
  return simple(
    generate: (random, size) {
      final actualMin = min ?? core.BigInt.from(-size);
      final actualMax = max ?? core.BigInt.from(size);
      assert(actualMax > actualMin);
      final bits = (actualMax - actualMin).bitLength;
      var bigInt = core.BigInt.zero;
      for (var i = 0; i < bits; i++) {
        bigInt = bigInt * core.BigInt.two;
        if (random.nextBool()) {
          bigInt += core.BigInt.one;
        }
      }
      return bigInt % (actualMax - actualMin) + actualMin;
    },
    shrink: (input) sync* {
      if (input > core.BigInt.zero) {
        yield input - core.BigInt.one;
      } else if (input < core.BigInt.zero) {
        yield input + core.BigInt.one;
      }
    },
  );
}