nextBoundedInt method

  1. @Possible({RangeError})
  2. @useResult
int nextBoundedInt(
  1. int min,
  2. int max
)

Generates a random integer uniformly distributed in the range, [min] <= value < [max].

Contract

Throws RangeError if min >= max.

Example

Random().nextBoundedInt(-1, 3); // -1 <= value < 3

Random().nextBoundedInt(3, 3) // throws RangeError

Implementation

@Possible({RangeError})
@useResult int nextBoundedInt(int min, int max) => switch (min < max) {
  true => nextInt(max - min) + min,
  false => throw RangeError.range(max, min, null, 'max'),
};