Returns a random element from a list.
Use Random.nextInt()
to generate a random integer between 0
and lst.length
and return the list element at that index.
代码实现
import 'dart:math';
T sample<T>(List<T> lst) {
return lst[Random().nextInt(lst.length)];
}
使用样例
sample([3, 7, 9, 11]); // 9