nextBoundedDouble method

  1. @Possible({RangeError})
  2. @useResult
double nextBoundedDouble(
  1. double min,
  2. double max
)

Generates a random double in the range, [min] <= value < [max].

The generated double is not guaranteed to be uniformly distributed.

Contract

Throws RangeError if:

  • min is infinite
  • max is infinite
  • min >= max
  • the resultant range is NaN
  • the resultant range is infinite

Example

Random().nextDouble(1.1, 1.3); // -1 <= value < 3

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

Random().nextBoundedInt(-double.maxFinite, double.maxFinite) // throws RangeError

Implementation details

This function scales the result of nextDouble. If the given range is sufficiently larger than [0.0 - 1.0), certain doubles in the given range will never be returned, Pigeonhole Principle.

Implementation

@Possible({RangeError})
@useResult double nextBoundedDouble(double min, double max) {
  _check(min, max);
  return nextDouble() * (max - min) + min;
}