ints method
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 positivemin
is greater than or equal tomax
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'),
};
}