randomItem<T> static method

T randomItem<T>(
  1. Iterable<T> iterable, [
  2. Random? random
])

Picks a random item from iterable and returns it.

Uses randomInt internally to select an item index.

An instance of Math.Random can optionally be passed to customize the random sample distribution.

Implementation

static T randomItem<T>(Iterable<T> iterable, [Math.Random? random]) {
  random = random ?? Math.Random();
  return iterable.elementAt(
    randomInt(0, iterable.length, random),
  );
}