shuffle function Null safety

void shuffle (
  1. List elements,
  2. [int start = 0,
  3. int? end,
  4. Random? random]
)

Shuffles a list randomly.

A sub-range of a list can be shuffled by providing start and end.

If start or end are omitted, they default to the start and end of the list.

If random is omitted, it defaults to a new instance of Random.

Implementation

void shuffle(List elements, [int start = 0, int? end, Random? random]) {
  random ??= Random();
  end ??= elements.length;
  var length = end - start;
  while (length > 1) {
    var pos = random.nextInt(length);
    length--;
    var tmp1 = elements[start + pos];
    elements[start + pos] = elements[start + length];
    elements[start + length] = tmp1;
  }
}