Skip to main content
Glama

generate_random_array

Generate a customizable random array for applications like batch number generation, sampling, test datasets, or task assignments. Specify length, value range, and duplication preferences for tailored results.

Instructions

Random Array Generator

Generate a random array of specified length

Args:
    salt (str, optional): Random number salt value. Defaults to "".
    array_length (int, optional): Array length. Defaults to 1.
    min_value (int, optional): Minimum value. Defaults to 0.
    max_value (int, optional): Maximum value. Defaults to 1000000.
    allow_duplicates (bool, optional): Allow duplicate values. Defaults to True.

Returns:
    str: JSON string containing the random array

Application Scenarios:
1. Batch random number generation
2. Random sampling
3. Test dataset generation
4. Random task assignment

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
allow_duplicatesNo
array_lengthNo
max_valueNo
min_valueNo
saltNo

Implementation Reference

  • main.py:67-90 (handler)
    MCP tool handler and registration for 'generate_random_array'. Includes input schema via type annotations and docstring. Delegates execution to the helper function in utils.py.
    @mcp.tool()
    async def generate_random_array(salt: str = "", array_length: int = 1, min_value: int = 0, 
                                  max_value: int = 1000000, allow_duplicates: bool = True) -> str:
        """Random Array Generator
        
        Generate a random array of specified length
        
        Args:
            salt (str, optional): Random number salt value. Defaults to "".
            array_length (int, optional): Array length. Defaults to 1.
            min_value (int, optional): Minimum value. Defaults to 0.
            max_value (int, optional): Maximum value. Defaults to 1000000.
            allow_duplicates (bool, optional): Allow duplicate values. Defaults to True.
        
        Returns:
            str: JSON string containing the random array
        
        Application Scenarios:
        1. Batch random number generation
        2. Random sampling
        3. Test dataset generation
        4. Random task assignment
        """
        return await random_array_generator(salt, array_length, min_value, max_value, allow_duplicates)
  • Core helper function implementing the random array generation logic using blockchain-derived entropy, seed derivation, and NumPy random functions.
    async def random_array_generator(array_length: int = 1, min_value: int = 0, max_value: int = 1000000, allow_duplicates: bool = True, salt: str="") -> Dict:
        """Random array generator"""
        random_num = await get_random_str()
        if not random_num:
            return {"error": "Failed to get random number"}
            
        request_id = generate_request_id(random_num)
        seed = _derive_seed(request_id, salt)
        np.random.seed(seed)
        
        if allow_duplicates:
            random_array = np.random.randint(min_value, max_value + 1, size=array_length).tolist()
        else:
            # Ensure no duplicates
            if (max_value - min_value + 1) < array_length:
                raise ValueError("Range is too small to generate non-duplicate values")
            
            random_array = np.random.choice(
                range(min_value, max_value + 1), 
                size=array_length, 
                replace=False
            ).tolist()
        
        result = {
            "requestId": request_id,
            "randomArray": random_array
        }
        
        return result
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/suxiongye/random-web3-mcp'

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