ints method
generates a list containing count
random integers whose values fall between min
(inclusive) and max
(exclusive)
only32bit
and only64bit
will limit generated numbers to 32-bit or 64-bit integers (respectively).
Setting both to true throws an ArgumentError
Omitting both or setting both to false will randomly generate elements of both types
If only32bit
is set to true, then the value passed for min
must be limited to a 32-bit max value; passing a number outside the 32-bit space will throw an ArgumentError
Implementation
List<$core.int> ints($core.int count, {$core.int min = 0, $core.int? max, bool only32bit = false, bool only64bit = false}) {
max ??= Int64.MAX_VALUE.toInt(); // -- force default even if `null` is explicitly passed
if (only32bit && only64bit) {
throw ArgumentError('Cannot set BOTH `only32bit` and `only64bit` to true');
}
$core.int gen32(i) => int32(max: $math.min(Int32.MAX_VALUE.toInt(), max!), min: min).toInt();
$core.int gen64(i) => int64(max: max, min: $math.max((Int32.MAX_VALUE.toInt() + 1), min)).toInt();
if (Int32.MAX_VALUE < min) {
if (only32bit && !only64bit) { // -- requested only32 bit, but with a min value greater than 32-bit max
throw ArgumentError('When `only32bit` is true, value of `min` must be a 32-bit number also');
}
only32bit = false;
only64bit = true;
}
if (Int32.MAX_VALUE.toInt() >= max) {
if (only64bit && !only32bit) {
throw ArgumentError('When `only64bit` is true, value of `max` must be a 64-bit number also');
}
only32bit = true;
only64bit = false;
}
$core.int Function($core.int i) callback;
if (only32bit && !only64bit) { // -- xor
callback = gen32;
} else if (only64bit && !only32bit) { // -- xor
callback = gen64;
} else { // -- mixed
callback = (i) => (this.boolean() ? gen32 : gen64)(i); // -- 50/50 chance
}
return List.generate(count, callback);
}