The source array to pick an element from
A randomly selected element from the array
const colors = ["red", "blue", "green", "yellow", "purple"];
const fruits = ["apple", "banana", "orange", "grape", "kiwi"];
RandomGenerator.pick(colors); // e.g. "blue"
RandomGenerator.pick(fruits); // e.g. "apple"
// Select random configuration options
const randomTheme = RandomGenerator.pick(["light", "dark", "auto"]);
const randomLocale = RandomGenerator.pick(["en", "ko", "ja", "zh"]);
// Choose random test scenarios
const testScenario = RandomGenerator.pick([
"happy_path",
"edge_case",
"error_condition",
"boundary_test",
]);
// Random user role assignment
const userRole = RandomGenerator.pick(["admin", "user", "moderator"]);
// Select random API endpoints for testing
const endpoints = ["/users", "/posts", "/comments", "/categories"];
const randomEndpoint = RandomGenerator.pick(endpoints);
contracts/common.md#principled-implementation One index maps a Math.random draw to a valid position, giving approximately uniform selection; an empty array has no element to return, so it raises a RangeError instead of returning undefined under the element type.
Randomly selects a single element from an array.
Chooses one element at random from the provided array using approximately uniform distribution inherited from Math.random. This is a convenience function equivalent to sampling with a count of 1, but returns the element directly rather than an array containing one element.
Processing cost: One bounded draw and one indexed lookup use constant work.