Dev.to WebDev πŸ›  Dev πŸ‘ 0 πŸ“– 3 min read

Exact Gacha Soft-Pity & M0W1 Probability in ~40 Lines of Vanilla JS (No Monte Carlo)

When building a browser-based pull planner for Zenless Zone Zero (Polychrome Calculator), I hit a classic probability modeling trade-off: Naive geometric formulas break completely once soft pity ramps up. Monte Carlo

Exact Gacha Soft-Pity & M0W1 Probability in ~40 Lines of Vanilla JS (No Monte Carlo)

When building a browser-based pull planner for Zenless Zone Zero (Polychrome Calculator), I hit a classic probability modeling trade-off:

  • Naive geometric formulas break completely once soft pity ramps up.
  • Monte Carlo simulations (e.g., 10,000 random trials per slider tick) produce jittery numbers and feel sluggish on mid-range phones.

Because hard pity is bounded (90 pulls for Exclusive Agents, 80 pulls for W-Engines), the entire state space is tiny. We can compute exact cumulative odds instantaneously in vanilla JavaScript using a small discrete Markov chain and 1D convolution.

1. Modeling the Soft-Pity Rate Curve

In Zenless Zone Zero, both limited channels follow piecewise-linear drop rates that hit 100% at hard pity:

Channel Base Rate Soft Pity Start Ramp / Pull Hard Pity Featured Rate
Exclusive Agent 0.6% Pull 74 +6.0% 90 50% (guaranteed after loss)
Signature W-Engine 1.0% Pull 65 +7.0% 80 75% (guaranteed after loss)
const MODELS = {
  agent:  { hard: 90, base: 0.006, soft: 74, inc: 0.06, feat: 0.50 },
  engine: { hard: 80, base: 0.010, soft: 65, inc: 0.07, feat: 0.75 },
};

function pullRate(model, n) {
  if (n >= model.hard) return 1;
  if (n < model.soft) return model.base;
  return Math.min(0.99, model.base + model.inc * (n - model.soft + 1));
}

2. Exact State-Transition DP (O(N Γ— hard))

At any step, the probability mass sits on states (pity, guaranteed, copies). Since we only advance one pull at a time:

  • On a miss (1 - q): pity + 1, guaranteed and copies stay the same.
  • On an S-Rank hit (q):
    • If guaranteed === 1, we win the featured unit (pity -> 0, guaranteed -> 0, copies + 1).
    • Otherwise, we win with probability model.feat (pity -> 0, guaranteed -> 0, copies + 1) or lose with probability 1 - model.feat (pity -> 0, guaranteed -> 1, copies unchanged).
function buildOddsCurve(model, startPity, startGuar, maxPulls, targetCopies = 2) {
  let states = new Map([[`${startPity}|${startGuar ? 1 : 0}|0`, 1]]);
  const cdf = [{ pulls: 0, p1: 0, pTarget: 0 }];

  for (let pull = 1; pull <= maxPulls; pull++) {
    const next = new Map();
    const add = (k, v) => next.set(k, (next.get(k) || 0) + v);

    for (const [key, prob] of states) {
      const [pi, g, c] = key.split('|').map(Number);
      if (c >= targetCopies) {
        add(key, prob);
        continue;
      }

      const q = pullRate(model, pi + 1);
      add(`${pi + 1}|${g}|${c}`, prob * (1 - q));

      const winKey = `0|0|${c + 1}`;
      if (g) {
        add(winKey, prob * q);
      } else {
        add(winKey, prob * q * model.feat);
        add(`0|1|${c}`, prob * q * (1 - model.feat));
      }
    }

    states = next;
    let p1 = 0, pTarget = 0;
    for (const [key, prob] of states) {
      const copies = Number(key.split('|')[2]);
      if (copies >= 1) p1 += prob;
      if (copies >= targetCopies) pTarget += prob;
    }
    cdf.push({ pulls: pull, p1, pTarget });
  }

  return cdf;
}

From the exact CDF array, the expected pull count is just the discrete sum of first differences sum(i * (cdf[i].p1 - cdf[i - 1].p1)) β€” zero sampling noise, deterministic to floating-point precision, and runs in under 1 ms on mobile.

3. Combining Agent + W-Engine (M0W1) via 1D Convolution

Players often want to know: "With 180 pulls saved, what are my exact odds of pulling **both* the Exclusive Agent (M0) and their Signature W-Engine (W1)?"*

Instead of expanding the Markov state to 2D (agentPity, enginePity), notice that the two banners are independent once you split your budget sequentially:

  1. Compute the exact first-hit PMF pmfAgent[a] for pulling M0 in exactly a pulls (0 <= a <= 180).
  2. Compute the exact first-hit PMF pmfEngine[w] for pulling W1 in exactly w pulls (0 <= w <= 160).
  3. Convolve them:
function convolvePmf(pmfA, pmfB) {
  const out = new Float64Array(pmfA.length + pmfB.length - 1);
  for (let i = 0; i < pmfA.length; i++) {
    if (!pmfA[i]) continue;
    for (let j = 0; j < pmfB.length; j++) {
      out[i + j] += pmfA[i] * pmfB[j];
    }
  }
  return out;
}

180 Γ— 160 = 28,800 multiply-adds β€” basically instant in V8 or JavaScriptCore, and it directly yields exact curves for M0W1, M1W1, or M2W1 side-by-side.

ZZZ M0W1 Soft-Pity Odds Calculator

4. Client-Side UX Details That Made It Feel Snappy

  • Zero backend / no account import: everything runs locally in the browser and persists in localStorage.
  • Shareable URL state: encoding current pity, saved pulls, and banner goal into query params lets players share an exact pull plan in Discord without screenshots.
  • No external charting library: rendering the soft-pity curve and progress bars with lightweight inline SVG + CSS keeps first paint fast and works cleanly as an installable PWA.

Live Demo & Open-Source Math Module

If you have built probability calculators for other games or RPG systems, I'd love to hear whether you went with closed-form DP, FFT convolution, or Monte Carlo!

πŸ“° 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.