random_choices
Choose random items from a list, with optional weights to influence selection probabilities. Ideal for sampling, simulations, or random assignments.
Instructions
Choose k items from population with replacement, optionally weighted.
Args: population: List of items to choose from k: Number of items to choose (default 1) weights: Optional weights for each item (default None for equal weights)
Returns: List of k chosen items
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| population | Yes | ||
| k | No | ||
| weights | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- src/random_number_mcp/tools.py:57-80 (handler)Core implementation of random_choices: calls random.choices() with validation for population, k, and weights.
def random_choices( population: list[Any], k: int = 1, weights: list[int | float] | None = None ) -> list[Any]: """Choose k items from population with replacement, optionally weighted. Args: population: List of items to choose from k: Number of items to choose (default 1) weights: Optional weights for each item (default None for equal weights) Returns: List of k chosen items Raises: ValueError: If population is empty, k < 0, or weights length doesn't match TypeError: If k is not an integer """ validate_list_not_empty(population, "population") validate_positive_int(k, "k") if weights is not None: validate_weights_match_population(population, weights) return random.choices(population, weights=weights, k=k) - src/random_number_mcp/server.py:41-65 (handler)MCP server registration of random_choices tool with app.tool() decorator; handles weights as optional JSON string.
@app.tool() def random_choices( population: list[str | int | float | bool], k: int = 1, weights: list[int | float] | str | None = None, ) -> list[str | int | float | bool]: """Choose k items from population with replacement, optionally weighted. Args: population: List of items to choose from k: Number of items to choose (default 1) weights: Optional weights for each item (default None for equal weights) Returns: List of k chosen items """ numeric_weights: list[int | float] | None = None if isinstance(weights, str): try: numeric_weights = json.loads(weights) except json.JSONDecodeError as e: raise ValueError(f"Invalid JSON string for weights: {weights}") from e else: numeric_weights = weights return tools.random_choices(population, k, numeric_weights) - src/random_number_mcp/server.py:41-41 (registration)@app.tool() decorator registration of the random_choices function as an MCP tool.
@app.tool() - src/random_number_mcp/utils.py:12-18 (helper)validate_positive_int: validates that k is a non-negative integer.
def validate_positive_int(value: int, name: str) -> None: """Validate that a value is a positive integer.""" if not isinstance(value, int): raise TypeError(f"{name} must be an integer, got {type(value).__name__}") if value < 0: raise ValueError(f"{name} must be non-negative, got {value}") - src/random_number_mcp/utils.py:26-34 (helper)validate_weights_match_population: validates weights list length matches population length.
def validate_weights_match_population( population: list[Any], weights: list[int | float] ) -> None: """Validate that weights list matches population length.""" if len(weights) != len(population): raise ValueError( f"Weights list length ({len(weights)}) must match " f"population length ({len(population)})" )