Dev.to WebDev 🛠 Dev 👁 0 📖 2 min read

At-least-one probabilities for very rare events in JavaScript: log1p, expm1 and a chest-count example

Questions like "how many tries until there is a 50% chance of at least one success?" come up in game drop rates, A/B tests and reliability maths. The textbook formula is 1 - (1 - p)^n. It is correct, but when p is tiny i

Questions like "how many tries until there is a 50% chance of at least one success?" come up in game drop rates, A/B tests and reliability maths. The textbook formula is 1 - (1 - p)^n. It is correct, but when p is tiny it loses precision in floating point. Here is a numerically stable version, with a real example: the published 1 in 137,500 per-chest rate of the Fortnite Pirate Token (Epic Games, November 7, 2026).

The stable form

function atLeastOne(p, n) {
  return -Math.expm1(n * Math.log1p(-p));
}

// chests (trials) needed to reach a given confidence; round up for whole trials
function chestsFor(confidence, p) {
  return Math.log(1 - confidence) / Math.log1p(-p);
}

Math.log1p(-p) computes ln(1 - p) without first forming 1 - p, and Math.expm1(x) computes e^x - 1 without cancellation near zero. Since (1 - p)^n = e^(n ln(1 - p)), the probability of at least one success is 1 - e^(n ln(1 - p)), which is -expm1(n * log1p(-p)).

Why the naive version can fail

With p = 1e-17 and n = 1e17, 1 - Math.pow(1 - p, n) returns 0, because 1 - 1e-17 rounds to exactly 1 in double precision. The stable form returns 0.6321205588285577, the correct value (about 1 - 1/e). For p = 1/137,500 the naive form still works; the stable form matters for much rarer events.

Numbers for p = 1/137,500

Running the functions above in Node:

Trials (chests) At least one
100 0.07%
2,000 1.44%
10,000 7.01%
50,000 30.49%
137,500 63.21%

chestsFor(0.5, 1/137500) is 95,307.39 and chestsFor(0.9, 1/137500) is 316,604.30, so 95,308 and 316,605 whole chests. These match the table in the browser-based Pirate Token odds calculator, which uses the same formula.

One more sanity check: (1 - 1/137500)^137500 is 0.367878, close to 1/e = 0.367879. After exactly 1/p trials, about 36.8% of runs still have no success, so the at-least-one chance is about 63.2%.

A related small tool

The Pirate Token rate is boosted during Saturday Treasure Hours, listed as 11 AM to 1 PM and 6 PM to 8 PM Pacific Time. Converting those to a local time zone is a classic Intl.DateTimeFormat exercise, and treasure-hours-converter does it in a few dozen lines with tests.

The calculator is an unofficial fan-made tool and is not affiliated with or endorsed by Epic Games.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.