nextInt method

  1. @override
int nextInt(
  1. int max
)
override

Generates a non-negative random integer uniformly distributed in the range from 0, inclusive, to max, exclusive.

Implementation note: The default implementation supports max values between 1 and (1<<32) inclusive.

Example:

var intValue = Random().nextInt(10); // Value is >= 0 and < 10.
intValue = Random().nextInt(100) + 50; // Value is >= 50 and < 150.

Implementation

@override
int nextInt(int max) {
  if (max <= 0) {
    throw ArgumentException.invalidOperationArguments(
      "nextInt",
      name: "max",
      reason: "Max must be greater than 0",
    );
  }

  final double fraction = nextUint32 / 4294967296.0;
  return (fraction * max).floor();
}