shuffle<T> function

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

Returns a shuffled copy of the iterable.

Implementation

List<T> shuffle<T>(Iterable<T> iterable, [math.Random? random]) {
  final list = iterable.toList();
  final rng = random ?? math.Random();

  for (int i = list.length - 1; i > 0; i--) {
    final j = rng.nextInt(i + 1);
    final temp = list[i];
    list[i] = list[j];
    list[j] = temp;
  }

  return list;
}