shuffle<T> function

List<T> shuffle<T>(
  1. List<T> array, [
  2. int i0 = 0,
  3. int? i1
])

Randomizes the order of array in-place over [i0, i1) using the Fisher-Yates shuffle and returns array.

Implementation

// DIVERGES(d3-array): adapt-array-fast-forms, uses Random.nextInt directly
// on the default shuffle hot path.
List<T> shuffle<T>(List<T> array, [int i0 = 0, int? i1]) {
  final start = i0;
  final end = i1 ?? array.length;
  var m = end - start;
  while (m > 0) {
    final i = _defaultRandom.nextInt(m--);
    final a = m + start;
    final b = i + start;
    final t = array[a];
    array[a] = array[b];
    array[b] = t;
  }
  return array;
}