generate_random_choice
Select random items from a list with cryptographically secure randomness. Define the number of choices and allow duplicates for customizable, unbiased results.
Instructions
Randomly select items from a given list using cryptographically secure randomness
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| allow_duplicates | No | Whether to allow duplicate selections | |
| choices | Yes | Array of items to choose from | |
| count | No | Number of items to select |
Implementation Reference
- src/index.ts:549-592 (handler)The primary handler function that executes the generate_random_choice tool logic. It validates input parameters, generates random selections from the choices array using cryptographically secure randomInt, supports optional duplicates, and returns a standardized JSON response.private async generateRandomChoice(args: any) { const choices = args.choices; const count = args.count ?? 1; const allowDuplicates = args.allow_duplicates ?? true; if (!Array.isArray(choices) || choices.length === 0) { throw new Error("Choices must be a non-empty array"); } if (count < 1) { throw new Error("Count must be at least 1"); } if (!allowDuplicates && count > choices.length) { throw new Error("Count cannot exceed choices length when duplicates are not allowed"); } const results: string[] = []; const availableChoices = [...choices]; for (let i = 0; i < count; i++) { const randomIndex = randomInt(0, availableChoices.length); const selected = availableChoices[randomIndex]; results.push(selected); if (!allowDuplicates) { availableChoices.splice(randomIndex, 1); } } return { content: [ { type: "text", text: JSON.stringify({ type: "random_choices", values: results, parameters: { choices, count, allow_duplicates: allowDuplicates }, timestamp: new Date().toISOString(), }, null, 2), }, ], }; }
- src/index.ts:187-214 (schema)Input schema and metadata definition for the generate_random_choice tool, used for validation and documentation in the tools list.name: "generate_random_choice", description: "Randomly select items from a given list using cryptographically secure randomness", inputSchema: { type: "object", properties: { choices: { type: "array", description: "Array of items to choose from", items: { type: "string", }, minItems: 1, }, count: { type: "integer", description: "Number of items to select", default: 1, minimum: 1, }, allow_duplicates: { type: "boolean", description: "Whether to allow duplicate selections", default: true, }, }, required: ["choices"], }, },
- src/index.ts:259-260 (registration)Registration in the tool dispatch switch statement within the CallToolRequestSchema handler, routing calls to the handler method.case "generate_random_choice": return await this.generateRandomChoice(args);