Random Number Generator API: The Complete Guide

Need to generate random numbers in your application? This guide covers everything you need to know about random number generation via API, including different types, security considerations, and implementation examples.

Types of Random Numbers

Random number generators (RNGs) produce sequences of numbers that lack predictable patterns. Different applications require different types of randomness, from simple pseudo-random numbers for games to cryptographically secure random numbers for security applications.

Understanding the difference between PRNG (Pseudo-Random Number Generator) and CSPRNG (Cryptographically Secure PRNG) is crucial for choosing the right approach.

Security Considerations

Key considerations when generating random numbers:

Pseudo-Random (PRNG)

Fast and suitable for games, simulations, and non-security applications. Deterministic given the seed value.

Cryptographically Secure (CSPRNG)

Required for passwords, tokens, encryption keys. Unpredictable even if previous output is known.

True Random (TRNG)

Based on physical phenomena. Maximum unpredictability but slower. Used for highest security applications.

Security Note: Always use CSPRNG for security-sensitive applications like session tokens, password generation, or cryptographic keys.

Using the Random Number API

TinyFn provides a simple endpoint to generate random numbers:

API Request
GET https://api.tinyfn.io/v1/random/number?min=1&max=100&count=5
Headers: X-API-Key: your-api-key
Response
{
  "numbers": [42, 17, 89, 3, 56],
  "min": 1,
  "max": 100,
  "count": 5,
  "type": "integer",
  "secure": true
}

Parameters

Parameter Type Description
min number Minimum value (inclusive)
max number Maximum value (inclusive)
count integer Number of random numbers to generate (default: 1)
type string Type: integer or float (default: integer)
unique boolean Ensure all numbers are unique (default: false)

Code Examples

JavaScript / Node.js

const response = await fetch(
  'https://api.tinyfn.io/v1/random/number?min=1&max=100&count=5',
  { headers: { 'X-API-Key': 'your-api-key' } }
);
const { numbers } = await response.json();
console.log(numbers); // [42, 17, 89, 3, 56]

Python

import requests

response = requests.get(
    'https://api.tinyfn.io/v1/random/number',
    params={'min': 1, 'max': 100, 'count': 5},
    headers={'X-API-Key': 'your-api-key'}
)
numbers = response.json()['numbers']
print(numbers)  # [42, 17, 89, 3, 56]

cURL

curl "https://api.tinyfn.io/v1/random/number?min=1&max=100&count=5" \
  -H "X-API-Key: your-api-key"

Common Use Cases

  • Gaming: Dice rolls, card shuffling, loot drops
  • Lotteries: Drawing winning numbers
  • Sampling: Statistical sampling and A/B testing
  • Simulations: Monte Carlo simulations and modeling
  • Randomization: Quiz questions, content rotation

Best Practices

  1. Use secure random for security: Never use basic PRNG for tokens or passwords
  2. Specify appropriate range: Set min/max to exactly what you need
  3. Use unique flag when needed: For lottery numbers, ensure no duplicates
  4. Cache appropriately: Don't cache random numbers unless specifically needed

Use via MCP

Your AI agent can call this tool directly via Model Context Protocol — no HTTP code needed. Add TinyFn to Claude Desktop, Cursor, or any MCP client:

{
  "mcpServers": {
    "tinyfn-random": {
      "url": "https://api.tinyfn.io/mcp/random/",
      "headers": {
        "X-API-Key": "your-api-key"
      }
    }
  }
}

See all random tools available via MCP in our Random MCP Tools for AI Agents guide.

Try the Random Number API

Get your free API key and start generating random numbers in seconds.

Get Free API Key

Ready to try TinyFn?

Get your free API key and start building in minutes.

Get Free API Key