Randomly selects a single element from an array.

Chooses one element at random from the provided array using uniform distribution. Each element has an equal probability of being selected. 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.

  const colors = ['red', 'blue', 'green', 'yellow', 'purple'];
const fruits = ['apple', 'banana', 'orange', 'grape', 'kiwi'];

RandomGenerator.pick(colors); // "blue"
RandomGenerator.pick(fruits); // "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);
  • Type Parameters

    • T

    Parameters

    • array: T[]

      The source array to pick an element from

    Returns T

    A randomly selected element from the array