random_sample
Select k unique elements from a population without replacement using the Random Number MCP server. Ideal for generating unbiased, random subsets efficiently.
Instructions
Choose k unique items from population without replacement.
Args: population: List of items to choose from k: Number of items to choose
Returns: List of k unique chosen items
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| k | Yes | ||
| population | Yes |
Implementation Reference
- src/random_number_mcp/server.py:82-93 (handler)MCP tool handler for 'random_sample' registered via @app.tool() decorator. Delegates execution to the helper in tools.py.@app.tool() def random_sample(population: list[Any], k: int) -> list[Any]: """Choose k unique items from population without replacement. Args: population: List of items to choose from k: Number of items to choose Returns: List of k unique chosen items """ return tools.random_sample(population, k)
- Core helper function implementing random sampling logic using Python's random.sample, including input validation.def random_sample(population: list[Any], k: int) -> list[Any]: """Choose k unique items from population without replacement. Args: population: List of items to choose from k: Number of items to choose Returns: List of k unique chosen items Raises: ValueError: If population is empty, k < 0, or k > len(population) TypeError: If k is not an integer """ validate_list_not_empty(population, "population") validate_positive_int(k, "k") if k > len(population): raise ValueError("Sample size k cannot be greater than population size") return random.sample(population, k)