generate_random_float
Generate cryptographically secure random floating-point numbers within a specified range, count, and precision for reliable data simulations or applications.
Instructions
Generate cryptographically secure random floating-point numbers
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| count | No | Number of random floats to generate | |
| max | No | Maximum value (exclusive) | |
| min | No | Minimum value (inclusive) | |
| precision | No | Number of decimal places to round to |
Implementation Reference
- src/index.ts:386-426 (handler)Core implementation of the generate_random_float tool handler. Uses crypto.randomBytes for secure randomness, scales to [min, max), applies precision rounding, generates multiple values if specified, and returns structured JSON response.private async generateRandomFloat(args: any) { const min = args.min ?? 0.0; const max = args.max ?? 1.0; const count = args.count ?? 1; const precision = args.precision ?? 6; if (min >= max) { throw new Error("Minimum value must be less than maximum value"); } if (count < 1 || count > 1000) { throw new Error("Count must be between 1 and 1000"); } if (precision < 1 || precision > 15) { throw new Error("Precision must be between 1 and 15"); } const results: number[] = []; for (let i = 0; i < count; i++) { // Generate cryptographically secure random float const randomBuffer = randomBytes(4); const randomValue = randomBuffer.readUInt32BE(0) / 0xFFFFFFFF; const scaledValue = min + (randomValue * (max - min)); results.push(parseFloat(scaledValue.toFixed(precision))); } return { content: [ { type: "text", text: JSON.stringify({ type: "random_floats", values: results, parameters: { min, max, count, precision }, timestamp: new Date().toISOString(), }, null, 2), }, ], }; }
- src/index.ts:79-108 (schema)Input schema for generate_random_float tool defining parameters: min (number, default 0.0), max (number, default 1.0 exclusive), count (1-1000, default 1), precision (1-15 decimals, default 6).inputSchema: { type: "object", properties: { min: { type: "number", description: "Minimum value (inclusive)", default: 0.0, }, max: { type: "number", description: "Maximum value (exclusive)", default: 1.0, }, count: { type: "integer", description: "Number of random floats to generate", default: 1, minimum: 1, maximum: 1000, }, precision: { type: "integer", description: "Number of decimal places to round to", default: 6, minimum: 1, maximum: 15, }, }, required: [], },
- src/index.ts:76-109 (registration)Tool registration in ListToolsRequestSchema handler, including name, description, and input schema.{ name: "generate_random_float", description: "Generate cryptographically secure random floating-point numbers", inputSchema: { type: "object", properties: { min: { type: "number", description: "Minimum value (inclusive)", default: 0.0, }, max: { type: "number", description: "Maximum value (exclusive)", default: 1.0, }, count: { type: "integer", description: "Number of random floats to generate", default: 1, minimum: 1, maximum: 1000, }, precision: { type: "integer", description: "Number of decimal places to round to", default: 6, minimum: 1, maximum: 15, }, }, required: [], }, },
- src/index.ts:251-252 (registration)Dispatch registration in CallToolRequestSchema switch statement, routing calls to generate_random_float to the handler method.case "generate_random_float": return await this.generateRandomFloat(args);