Returns a random element from the given array.
The type of the elements in the array.
The array to sample from.
A random element from the given array, or undefined if the array is empty.
undefined
import { sample } from "@std/collections/sample";import { assertArrayIncludes } from "@std/assert";const numbers = [1, 2, 3, 4];const random = sample(numbers);assertArrayIncludes(numbers, [random]); Copy
import { sample } from "@std/collections/sample";import { assertArrayIncludes } from "@std/assert";const numbers = [1, 2, 3, 4];const random = sample(numbers);assertArrayIncludes(numbers, [random]);
Returns a random element from the given array.