ints method

  1. @Possible({RangeError})
  2. @useResult
Stream<int> ints({
  1. int? length,
  2. int min = 0,
  3. required int max,
})

Returns a Stream of length that produces random integers in the range, [min] <= value < [max].

If length is not given, returns an infinite Stream instead.

Contract

Throws RangeError if:

  • length is not positive
  • min is greater than or equal to max

Example

Random.ints(length: 5, min: 0, max: 3); // 5 values, 0 <= value < 3

Random.ints(length: 1, min: 3, max: 2); // throws RangeError

Implementation

@Possible({RangeError})
@useResult Stream<int> ints({int? length, int min = 0, required int max}) { // ignore: always_put_required_named_parameters_first
  if (length != null) {
    RangeError.checkNotNegative(length, 'length');
  }

  return switch (min < max) {
    true => _generate(length, () => nextInt(max - min) + min),
    false => throw RangeError.range(max, min, null, 'max'),
  };
}