random method

E random()

Returns a random element from this set.

Throws a StateError if the set is empty.

Example:

final set = {1, 2, 3, 4, 5};
print(set.random()); // Outputs a random element from the set

Implementation

E random() {
  if (isEmpty) throw StateError('Set is empty.');
  final random = Random();
  return elementAt(random.nextInt(length));
}