shuffle method

  1. @override
void shuffle([
  1. Random? random
])
override

Shuffles the elements of this list randomly.

final numbers = <int>[1, 2, 3, 4, 5];
numbers.shuffle();
print(numbers); // [1, 3, 4, 5, 2] OR some other random result.

Implementation

@override
void shuffle([Random? random]) {
  Random rand = random ?? Random();
  final sliceLength = len();
  for (int i = _start; i < _end; i++) {
    final temp = _list[i];
    final swapWith = _start + rand.nextInt(sliceLength);
    _list[i] = _list[swapWith];
    _list[swapWith] = temp;
  }
}