Skip to main content
Glama
zazencodes

Random Number MCP

by zazencodes

random_choices

Select k items from a population with optional weighted probabilities. Use this tool for random sampling with replacement in applications requiring weighted or unbiased selections.

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

TableJSON Schema
NameRequiredDescriptionDefault
kNo
populationYes
weightsNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • MCP tool handler for 'random_choices'. Handles optional JSON string weights and delegates core logic to tools.random_choices.
    @app.tool()
    def random_choices(
        population: list[Any],
        k: int = 1,
        weights: list[int | float] | str | 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
        """
        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)
  • Core helper function implementing random choices logic using Python's random.choices, with input validation.
    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)

Tool Definition Quality

Score is being calculated. Check back soon.

Install Server

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/zazencodes/random-number-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server