Documentation
    Preparing search index...
    • 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.

      Type Parameters

      • T

      Parameters

      • array: readonly T[]

        The source array to pick an element from

      Returns T

      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);

      RangeError when the array is empty

      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.

      contracts/common.md#clear-and-simple-design One guard and one draw.

      contracts/common.md#prohibited-implementation-shortcuts The guard states the contract for the empty array and no default element is invented.

      contracts/common.md#meaningful-documentation The comment states the approximately uniform selection, the relation to sampling one element, and the RangeError for an empty array, with examples.