nextUint52 method

int nextUint52([
  1. int? max
])

Returns a random cross-platform unsigned 52-bit integer.

Note that 52-bit integers, unlike 64-bit integers, can always be accurately represented in JavaScript.

Implementation

int nextUint52([int? max]) {
  if (max != null && (max < 0 || max > _bit52)) {
    throw ArgumentError.value(max, 'max');
  }
  var attempts = 0;
  while (true) {
    final high = nextUint32();
    final low = nextUint32();
    final x = ((_bit32 * (0xFFFFF & high)) + low);
    if (max == null) {
      return x;
    }
    if (attempts == 64 || x < _bit52 ~/ max * max) {
      return x % max;
    }
    attempts++;
  }
}