nextWeightedBool method

  1. @Possible({RangeError})
  2. @useResult
bool nextWeightedBool(
  1. double probability
)

Returns a weighted boolean value based on probability.

The probability of this function returning true increases with the value of probability.

Contract

Throws RangeError if: probability < 0 or 1 < probability probability is infinite [probability is NaN

Example

Random().nextWeightedBool(1); // Always returns true

Random().nextWeightedBool(0); // Always returns false

Implementation

@Possible({RangeError})
@useResult bool nextWeightedBool(double probability) {
  if (!(0 <= probability && probability <= 1)) {
    throw RangeError.range(probability, 0, 1, 'probability');
  }

  return nextDouble() < probability;
}