shuffle_array
Randomly shuffle arrays ensuring fairness using the Fisher-Yates algorithm. Ideal for shuffling game elements, generating random orderings, grouping tasks, or preparing datasets. Supports optional salt for enhanced randomness.
Instructions
Random Array Shuffler
Randomly shuffle the input array, ensuring each element has an equal probability of appearing in any position.
Uses Fisher-Yates shuffle algorithm to ensure fairness.
Args:
input_array (List): Array to be shuffled, elements can be of any type
salt (str, optional): Random number salt value for increased randomness. Defaults to "".
Returns:
str: JSON string containing the shuffled array, formatted as:
{
"requestId": "Generated request ID",
"shuffledArray": [Shuffled array]
}
Application Scenarios:
1. Game shuffling (playing cards, mahjong tiles)
2. Random ordering (question order, playlist)
3. Random grouping (team assignment, experiment grouping)
4. Data shuffling (training dataset, test cases)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| input_array | Yes | ||
| salt | No |
Implementation Reference
- main.py:240-264 (handler)MCP tool handler and registration for 'shuffle_array'. Thin wrapper that delegates to the random_shuffler helper function from utils.@mcp.tool() async def shuffle_array(input_array: List, salt: str = "") -> str: """Random Array Shuffler Randomly shuffle the input array, ensuring each element has an equal probability of appearing in any position. Uses Fisher-Yates shuffle algorithm to ensure fairness. Args: input_array (List): Array to be shuffled, elements can be of any type salt (str, optional): Random number salt value for increased randomness. Defaults to "". Returns: str: JSON string containing the shuffled array, formatted as: { "requestId": "Generated request ID", "shuffledArray": [Shuffled array] } Application Scenarios: 1. Game shuffling (playing cards, mahjong tiles) 2. Random ordering (question order, playlist) 3. Random grouping (team assignment, experiment grouping) 4. Data shuffling (training dataset, test cases) """ return await random_shuffler(input_array, salt)
- utils.py:511-540 (helper)Core shuffling logic using blockchain-derived random seed with NumPy's Fisher-Yates shuffle implementation (np.random.shuffle). Generates request ID and returns JSON-compatible dict.async def random_shuffler(input_array: List, salt: str="") -> Dict: """ Random array shuffler Randomly shuffle the input array Args: input_array: Array to shuffle salt: Optional salt value for additional randomness Returns: Dict containing shuffled array """ 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) # Copy array to avoid modifying original shuffled_array = input_array.copy() np.random.shuffle(shuffled_array) result = { "requestId": request_id, "shuffledArray": shuffled_array } return result