generateRandomBigInt static method

BigInt generateRandomBigInt(
  1. BigInt min,
  2. BigInt max, [
  3. Random? random
])

Implementation

static BigInt generateRandomBigInt(BigInt min, BigInt max, [Random? random]) {
  random ??= Random.secure();
  final maxBytes = (max.bitLength + 7) >> 3;
  final result = Uint8List(maxBytes);
  for (int i = 0; i < maxBytes; i++) {
    result[i] = random.nextInt(256);
  }
  BigInt bigInt = BigInt.parse(
      result.reversed
          .toList()
          .map((byte) => byte.toRadixString(16).padLeft(2, '0'))
          .join(''),
      radix: 16);
  bigInt &= max; // Ensures the number is within the range.

  return bigInt < min
      ? min
      : bigInt; // Ensures the number is not below the minimum.
}