Gets a random element from an array.
从数组中获取一个随机元素。
The array to sample from. 要取样的数组
A random element from the array, or undefined if the array is empty. 数组中的随机元素,如果数组为空则返回 undefined
O(1) time complexity - direct index access
O(1) 时间复杂度 - 直接索引访问
sample([1, 2, 3, 4, 5])// => 2 (random element from the array) Copy
sample([1, 2, 3, 4, 5])// => 2 (random element from the array)
sample([])// => undefined Copy
sample([])// => undefined
Gets a random element from an array.
从数组中获取一个随机元素。