tRNG.info

The trng.info API provides free access to quantum-based true random numbers via a simple REST interface. No registration required for basic usage.

Quick Start

Generate a random integer between 1 and 100:

JavaScript
fetch('https://api.trng.info/random/int?min=1&max=100')
  .then(response => response.json())
  .then(data => console.log(data.value));
cURL
curl "https://api.trng.info/random/int?min=1&max=100"

Base URL

https://api.trng.info

All endpoints are accessed via GET requests over HTTPS. Responses are returned in JSON format.

Authentication

Basic usage requires no authentication. For higher rate limits, request a free API key.

API keys can be passed in two ways:

Header (Recommended)

X-API-Key: your-api-key-here

URL Parameter

https://api.trng.info/random/int?min=1&max=100&api_key=your-api-key-here

To request an API key, contact info@trng.info.

Rate Limits

Access Type Limit Authentication
Public (no key) 50 requests/minute None required
With API key Custom (per agreement) API key required
Web interface (trng.info) No limit N/A

Rate limit information is included in response headers:

  • X-RateLimit-Limit — Maximum requests allowed
  • X-RateLimit-Remaining — Requests remaining in current window
  • X-RateLimit-Reset — Unix timestamp when the limit resets

Exceeding the rate limit returns HTTP 429 Too Many Requests.

Endpoints

GET /random/int

Generate a random integer within a specified range.

Parameter Type Default Description
min integer 0 Minimum value (inclusive)
max integer 1 Maximum value (inclusive)

Response:

{"value": 42}

GET /random/float

Generate a random floating-point number within a specified range.

Parameter Type Default Description
min float 0.0 Minimum value
max float 1.0 Maximum value

Response:

{"value": 0.7284653829}

GET /random/bit

Generate a random bit (0 or 1).

Response:

{"bit": 1}

GET /random/bool

Generate a random boolean value (true or false).

Response:

{"value": true}

GET /random/bytes

Generate random bytes in hexadecimal format.

Parameter Type Default Description
length integer 32 Number of bytes to generate

Response:

{"data": "a3f5c8d2e1b4f6a9..."}

GET /random/choice

Select a random item from a list of choices.

Parameter Type Required Description
choices string (multiple) Yes Items to choose from. Pass multiple times: ?choices=A&choices=B&choices=C

Response:

{"choice": "B", "index": 1}

GET /random/shuffle

Randomly shuffle a list of items using the Fisher-Yates algorithm.

Parameter Type Required Description
items string (multiple) Yes Items to shuffle. Pass multiple times: ?items=A&items=B&items=C

Response:

{"shuffled": ["C", "A", "D", "B"]}

GET /random/sample

Sample items from a list without replacement.

Parameter Type Required Description
items string (multiple) Yes Items to sample from. Pass multiple times.
count integer Yes Number of items to sample (must be ≤ number of items)

Response:

{"sample": ["C", "A"]}

Error Handling

The API uses standard HTTP status codes. Errors return a JSON body with details:

{"error": "Bad Request", "message": "Parameter 'min' must be less than 'max'"}
Status Code Meaning Common Cause
200 OK Successful request
400 Bad Request Invalid or missing parameters
401 Unauthorized Missing or invalid API key
429 Too Many Requests Rate limit exceeded
500 Internal Server Error Server or hardware issue

Code Examples

Python

Python
import requests

API_BASE = "https://api.trng.info"

# Random integer (1-100)
response = requests.get(
    f"{API_BASE}/random/int",
    params={'min': 1, 'max': 100}
)
print(response.json()['value'])  # 42

# Random choice from list
response = requests.get(
    f"{API_BASE}/random/choice",
    params={'choices': ['red', 'blue', 'green']}
)
print(response.json()['choice'])  # 'blue'

# Random bytes for cryptography (32 bytes = 256 bits)
response = requests.get(
    f"{API_BASE}/random/bytes",
    params={'length': 32}
)
secure_token = response.json()['data']
print(secure_token)  # 'a3f5c8d2e1b4f6a9...'

JavaScript (Node.js / Browser)

JavaScript
const API_BASE = "https://api.trng.info";

// Random float (0-1)
const res1 = await fetch(`${API_BASE}/random/float?min=0&max=1`);
const data1 = await res1.json();
console.log(data1.value);  // 0.7284653829

// Shuffle a list
const res2 = await fetch(
  `${API_BASE}/random/shuffle?items=A&items=B&items=C`
);
const data2 = await res2.json();
console.log(data2.shuffled);  // ['C', 'A', 'B']

// Error handling
try {
  const res = await fetch(`${API_BASE}/random/int?min=1&max=100`);
  if (!res.ok) throw new Error(`HTTP ${res.status}`);
  const data = await res.json();
  console.log(data.value);
} catch (error) {
  console.error('API Error:', error);
}

R

R
library(httr)
library(jsonlite)

API_BASE <- "https://api.trng.info"

# Random integer
response <- GET(
  paste0(API_BASE, "/random/int"),
  query = list(min = 1, max = 100)
)
content(response)$value  # 42

# Random sample (without replacement)
response <- GET(
  paste0(API_BASE, "/random/sample"),
  query = list(
    items = c("A", "B", "C", "D", "E"),
    count = 3
  )
)
content(response)$sample  # ["C", "A", "E"]

cURL

cURL
# Random integer
curl "https://api.trng.info/random/int?min=1&max=100"

# Random boolean
curl "https://api.trng.info/random/bool"

# Random bytes (16 bytes in hex)
curl "https://api.trng.info/random/bytes?length=16"

# With API key
curl -H "X-API-Key: your-key" "https://api.trng.info/random/int?min=1&max=100"

jsPsych (Stimulus Randomization)

JavaScript
// Shuffle trial stimuli using TRNG before starting a jsPsych experiment
const stimuli = ['img/face1.png', 'img/face2.png', 'img/face3.png',
                 'img/scene1.png', 'img/scene2.png', 'img/scene3.png'];

const res = await fetch(
  'https://api.trng.info/random/shuffle?'
  + stimuli.map(s => 'items=' + encodeURIComponent(s)).join('&')
);
const { shuffled } = await res.json();

// Build jsPsych timeline with TRNG-shuffled order
const trials = shuffled.map(img => ({
  type: jsPsychImageKeyboardResponse,
  stimulus: img,
  choices: ['f', 'j'],
  prompt: '<p>Face (F) or Scene (J)?</p>'
}));

jsPsych.run([...trials]);

Secure Token Generation (Python)

Python
import requests

# Generate 32-byte secure token for API key
response = requests.get(
    'https://api.trng.info/random/bytes',
    params={'length': 32}
)
secure_token = response.json()['data']
# Use for API key, session ID, etc.

Support

For API key requests, bug reports, or questions, contact us at info@trng.info.

Try It Now

Use the web interface to generate random numbers without any code.

Launch App Learn More