Odds Calculator API

Calculate the number of attempts needed to achieve target probabilities for events with given odds

Overview

The Odds Calculator API helps you determine how many attempts (tickets, tries, etc.) are required to achieve specific probability thresholds of success for events with known odds. Given odds of "1 in N", the API calculates how many attempts you need to have a 50%, 80%, and 95% chance of at least one success.

Example Use Case: If a lottery ticket has a 1 in 1,000 chance of winning, how many tickets do you need to buy to have an 80% chance of winning at least once? This API provides the answer.

Endpoint

POST https://www.promptbox.cn/api/odds.php

Request

Headers

Header Value Required
Content-Type application/json Yes

Request Body Parameters

Parameter Type Description Required
odds number The odds ratio (N in "1 in N"). Must be a positive number. Example: for 1 in 1,000 odds, use 1000. Yes

Request Example

{
  "odds": 1000
}

Response

Success Response (200 OK)

Returns the number of attempts needed for each probability threshold.

Field Type Description
odds_one_in number The odds ratio that was used in the calculation (rounded to nearest integer)
attempts_for_50_percent integer Number of attempts needed for a 50% probability of at least one success
attempts_for_80_percent integer Number of attempts needed for an 80% probability of at least one success
attempts_for_95_percent integer Number of attempts needed for a 95% probability of at least one success

Success Response Example

{
  "odds_one_in": 1000,
  "attempts_for_50_percent": 693,
  "attempts_for_80_percent": 1609,
  "attempts_for_95_percent": 2996
}

Error Response (400 Bad Request)

Returns an error message when the request is invalid.

Field Type Description
error string Description of what went wrong

Error Response Examples

{
  "error": "Missing or invalid numeric parameter: 'odds'."
}
{
  "error": "Odds must be a positive number."
}
{
  "error": "Invalid or empty JSON input."
}

Mathematical Background

The API uses probability theory to calculate the required number of attempts. For an event with probability p = 1/N of success on each attempt:

The API iteratively calculates the minimum number of attempts n where the probability of at least one success meets or exceeds each threshold (50%, 80%, 95%).

Note: This calculation assumes independent events (each attempt has the same probability regardless of previous outcomes) and that you're looking for at least one success, not a specific number of successes.

Code Examples

JavaScript (Fetch API)

fetch('https://www.promptbox.cn/api/odds.php', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    odds: 1000
  })
})
.then(response => response.json())
.then(data => {
  console.log('For 50% chance:', data.attempts_for_50_percent, 'attempts');
  console.log('For 80% chance:', data.attempts_for_80_percent, 'attempts');
  console.log('For 95% chance:', data.attempts_for_95_percent, 'attempts');
})
.catch(error => console.error('Error:', error));

Python (requests)

import requests
import json

url = 'https://www.promptbox.cn/api/odds.php'
data = {'odds': 1000}

response = requests.post(url, json=data)
result = response.json()

if response.status_code == 200:
    print(f"For 50% chance: {result['attempts_for_50_percent']} attempts")
    print(f"For 80% chance: {result['attempts_for_80_percent']} attempts")
    print(f"For 95% chance: {result['attempts_for_95_percent']} attempts")
else:
    print(f"Error: {result['error']}")

cURL

curl -X POST https://www.promptbox.cn/api/odds.php \
  -H "Content-Type: application/json" \
  -d '{"odds": 1000}'

PHP

<?php
$url = 'https://www.promptbox.cn/api/odds.php';
$data = ['odds' => 1000];

$options = [
    'http' => [
        'header'  => "Content-Type: application/json\r\n",
        'method'  => 'POST',
        'content' => json_encode($data)
    ]
];

$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
$result = json_decode($response, true);

if (isset($result['attempts_for_50_percent'])) {
    echo "For 50% chance: {$result['attempts_for_50_percent']} attempts\n";
    echo "For 80% chance: {$result['attempts_for_80_percent']} attempts\n";
    echo "For 95% chance: {$result['attempts_for_95_percent']} attempts\n";
} else {
    echo "Error: {$result['error']}\n";
}
?>

Use Cases

Limitations and Considerations

Rate Limits and Best Practices

Support and Contact

For questions, issues, or feature requests regarding this API: