Hypergeometric probability
Drawing 5 cards from a 52-card deck with 4 aces gives exactly 2 aces in close to 4 percent of hands: 6 times 17296 of the 2598960 possible hands. Enter population, successes, draws and target to get it exactly. Free in your browser.
- Free
- Runs in your browser
- No sign-up
- Your data stays on your device
Run — free
Worked example
- Population size
- 52
- K
- 4
- N
- 5
- K
- 2
- Probability
- 0.0399298181
- Coefficient success
- 6
- Coefficient failure
- 17296
- Coefficient total
- 2598960
Sampling without replacement is the everyday setting for cards, quality-control lots, and urn models: each draw changes the composition of what remains, so successive trials are not independent. When a finite population of size N contains exactly K success states and you draw n items at once or in sequence without putting them back, the chance of observing exactly k successes follows the hypergeometric probability mass function. That formula multiplies the ways to choose k successes from the K good items by the ways to choose the remaining draws from the non-successes, then divides by the ways to choose any sample of size n from the whole population. This capability accepts N, K, n, and k, validates that the parameters are consistent with a legal without-replacement experiment, and returns the probability together with exact binomial coefficients as decimal strings, a short formula string, and the feasible range of k for those N, K, and n. Impossible combinations such as more successes in the sample than exist in the population are rejected with a clear validation error instead of a silent zero.
How hypergeometric probability works without replacement
Start with a finite population of N distinct items of which exactly K are labeled success (aces, defectives, tagged fish, winning tickets). You draw a sample of size n without replacement, so the order of draws does not create new combinations: every subset of size n is equally likely. Let X be the number of successes in that sample.
Then X follows a hypergeometric distribution, and the probability of observing exactly k successes is C(K, k) times C(N minus K, n minus k) divided by C(N, n), where each C(a, b) is a binomial coefficient counting combinations. The support of X is not simply zero through n: k cannot exceed K or n, and it cannot fall below max(0, n minus (N minus K)), because you may be forced to pick some successes once the non-success pool is exhausted.
Classic checks include a 52-card deck with four aces, drawing five cards, and asking for exactly two aces: C(4, 2) times C(48, 3) over C(52, 5). Another textbook case is an urn with twenty balls of which eight are red, drawing five, and asking for exactly three red.
Edge cases matter for correctness: if n is zero then only k equals zero has probability one; if K is zero then only k equals zero is possible when n is positive; if k lies outside the feasible interval the parameters are inconsistent for this model. This implementation never uses Monte Carlo sampling, random seeds, wall-clock time, or network lookups: it computes exact binomial coefficients with BigInt arithmetic and evaluates the probability in a numerically stable way so intermediate products stay well-behaved for larger N.
Where hypergeometric probabilities show up in real work
Introductory statistics and discrete-math courses constantly ask for the chance of exactly k marked items in a hand or sample drawn without replacement, from poker-style card problems to fish-tag recapture sketches that motivate finite-population sampling. Quality engineers use the same mass function when a lot of size N contains K defectives and an inspector draws n units, needing the probability of finding exactly k bad pieces before accepting or rejecting the lot.
Ecologists and survey designers compare observed tag recoveries against hypergeometric expectations when the population of tagged individuals is known and sampling is without replacement. Data scientists validate Monte Carlo simulators of finite urns and card shuffles by comparing sampled frequencies against the closed-form hypergeometric probability for modest N before scaling simulations. Educators build answer keys and auto-graders that need a single deterministic value rather than a random simulation that would change between runs.
Game designers and probability demos often expose deck and urn defaults while still allowing full control of N, K, n, and k for teaching dependence on population composition. If you later need the cumulative distribution P(X less than or equal to k), Fisher's exact test tables, inverse quantiles, or independent-trial binomial odds with replacement, treat those as separate search intents rather than overloading this landing.
Keep N inside the documented bound so free-tier latency stays predictable, and remember that floating-point probability is ideal for ranking and thresholds while the string coefficients remain exact for combinatorial checks.
What you can do with it
Cards, urns, and statistics homework
Compute exact without-replacement probabilities for hands, urn draws, and textbook hypergeometric problems with deterministic answer keys.
Simulator and Monte Carlo checks
Validate finite-population sampling code against the closed-form hypergeometric PMF for modest N before trusting large-scale draws.
FAQ
What formula does this use?
The hypergeometric probability mass function: P(X=k) = C(K,k) × C(N−K, n−k) / C(N,n), for sampling without replacement from a finite population.
How is this different from binomial coin-flip probability?
Binomial (with replacement or independent trials) keeps the success chance fixed each trial. Hypergeometric models without-replacement draws from a finite population, so each draw changes the remaining composition.
What happens if the parameters are inconsistent?
Examples: K greater than N, n greater than N, k greater than n or K, or k outside the feasible support max(0, n−(N−K))…min(n, K).
Are the binomial coefficients exact?
Yes. C(K,k), C(N−K, n−k), and C(N,n) are computed with BigInt and returned as decimal strings so every digit is exact within the supported N range.
What does it cost?
The browser tool is free and runs locally.
Related tools
For developers — API access
Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.
API endpoint
Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.
Call it from your stack
curl -X POST https://api.kit.forhosting.com/prob/hypergeometric \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"N":52,"K":4,"n":5,"k":2}'const res = await fetch("https://api.kit.forhosting.com/prob/hypergeometric", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"N": 52,
"K": 4,
"n": 5,
"k": 2
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/prob/hypergeometric",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"N": 52,
"K": 4,
"n": 5,
"k": 2
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/prob/hypergeometric", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"N":52,"K":4,"n":5,"k":2}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"N":52,"K":4,"n":5,"k":2}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/prob/hypergeometric", body)
req.Header.Set("Authorization", "Bearer "+os.Getenv("KIT_KEY"))
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)Example request
{
"N": 52,
"K": 4,
"n": 5,
"k": 2
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "prob.hypergeometric",
"status": "queued",
"_links": {
"result": "/tasks/tsk_…/result"
}
}The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.
Pricing
Published price — no tokens, no invented credits. A failed task is never charged.
Limits
min_N | 0 |
max_N | 10000 |
min_K | 0 |
min_n | 0 |
min_k | 0 |
Errors
| HTTP | Code | Meaning |
|---|---|---|
401 | unauthorized | Missing or invalid API key. |
402 | insufficient_balance | Your balance doesn't cover the task price. |
404 | unknown_type | That task type doesn't exist. |
429 | rate_limited | Too many requests. Use the webhook instead of polling. |
Technical notes
What you send and what you get back
Provide four required integers: N (population size; aliases population, pop, or population_size), K (successes in the population; aliases successes_pop, good, marked, or population_successes), n (draws or sample size; aliases draws, sample, or sample_size), and k (successes in the sample; aliases successes, observed, or sample_successes). Missing fields, non-integers, decimals where integers are required, negative values, K greater than N, n greater than N, k greater than n, k greater than K, and any k outside the feasible hypergeometric support are rejected with invalid_input and are not charged. Error messages name the offending field or the inconsistent combination so form clients and scripts can correct the request without guessing. On success the response always includes the normalized N, K, n, and k; probability as a floating-point number in zero to one; coefficient_success, coefficient_failure, and coefficient_total as exact decimal strings for C(K, k), C(N minus K, n minus k), and C(N, n); formula as a short display string of the hypergeometric ratio; and feasible_min_k plus feasible_max_k describing the legal support for k under those parameters. Example checks: N equals 52, K equals 4, n equals 5, k equals 2 yields the classic two-ace probability and coefficient triple for a five-card hand; N equals 20, K equals 8, n equals 5, k equals 3 yields a standard urn problem; N equals 10, K equals 3, n equals 5, k equals 4 yields invalid_input because four successes cannot appear when only three exist in the population; N equals 50, K equals 60 is rejected because K exceeds N. Pricing for successful API calls is the published base of $0.002 per item; validation failures do not consume prepaid balance.
Where hypergeometric probabilities show up in real work
Because this capability is a pure parse engine, API pricing is the published base of $0.002 per successful item and failed validation does not consume balance.
Integration use cases
Lot inspection and quality control
Given a lot of size N with K defectives and a sample of n units, get P(exactly k defectives) for accept/reject teaching examples and fixtures.