countFromFloat method

int countFromFloat(
  1. double range
)

Gets a random integer count within the given floating point range.

The decimal portion of the range is treated as a fractional chance of returning the next higher integer value. For example:

countFromFloat(10.2);

This has an 80% chance of returning 10 and a 20% chance of returning 11.

This is particularly useful when the range is less than one, because it gives you some chance of still producing one instead of always rounding down to zero.

Implementation

int countFromFloat(double range) {
  var count = range.floor();
  if (rng.float(1.0) < range - count) count++;
  return count;
}