惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
云风的 BLOG
云风的 BLOG
小众软件
小众软件
雷峰网
雷峰网
博客园 - 【当耐特】
V
V2EX
WordPress大学
WordPress大学
IT之家
IT之家
Last Week in AI
Last Week in AI
罗磊的独立博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
Visual Studio Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
有赞技术团队
有赞技术团队
The Cloudflare Blog
Jina AI
Jina AI
博客园 - 司徒正美
阮一峰的网络日志
阮一峰的网络日志
博客园 - 聂微东
大猫的无限游戏
大猫的无限游戏
博客园 - 三生石上(FineUI控件)
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
What 123 million simulated CS2 case openings taught me ab...
graysonwerne · 2026-05-10 · via DEV Community

I run case-sim.com, a free CS2 case opening simulator. As of this week the global counter ticked past 123 million openings. That's a weirdly humbling number — it's also enough rolls that any subtle bug in my probability code would have been screaming at me for years.

I want to share what I actually learned building this thing, because most "how to model case openings" tutorials I've seen are wrong in at least one quiet, important way. If you're building anything with weighted RNG — loot tables, gacha, slot mechanics, A/B traffic splitting — some of this will probably save you a bug.

This is going to be code-heavy. Sorry/not sorry.

The drop rate model (and the off-by-one I shipped for two weeks)

Valve disclosed CS2's case tiers back in 2017 and they haven't changed:

Tier Color Drop chance
Mil-Spec Blue 79.92%
Restricted Purple 15.98%
Classified Pink 3.20%
Covert Red 0.64%
Knife/Glove Gold 0.26%

Add those up: exactly 100%. That's not a coincidence — that's an invariant you should be enforcing in tests, not assuming.

The naive implementation is simple:

const TIER_WEIGHTS = {
  milSpec:    0.7992,
  restricted: 0.1598,
  classified: 0.0320,
  covert:     0.0064,
  rare:       0.0026, // knife/glove
};

function rollTier() {
  const r = Math.random();
  let cumulative = 0;
  for (const [tier, weight] of Object.entries(TIER_WEIGHTS)) {
    cumulative += weight;
    if (r < cumulative) return tier;
  }
  return 'milSpec'; // floating-point fallback
}

Enter fullscreen mode Exit fullscreen mode

That fallback at the bottom is not me being paranoid. 0.7992 + 0.1598 + 0.0320 + 0.0064 + 0.0026 should equal 1.0, but in JavaScript:

0.7992 + 0.1598 + 0.0320 + 0.0064 + 0.0026
// 0.9999999999999999

Enter fullscreen mode Exit fullscreen mode

If Math.random() returns 0.99999999... and your cumulative caps out at 0.9999999999999999, you fall off the end. With 123M rolls that bug fires roughly... a lot. So you either return a fallback or you switch to integer weights:

const TIER_WEIGHTS_INT = {
  milSpec:    7992,
  restricted: 1598,
  classified:  320,
  covert:       64,
  rare:         26,
}; // sums to exactly 10000

function rollTier() {
  const r = Math.floor(Math.random() * 10000);
  let cumulative = 0;
  for (const [tier, weight] of Object.entries(TIER_WEIGHTS_INT)) {
    cumulative += weight;
    if (r < cumulative) return tier;
  }
}

Enter fullscreen mode Exit fullscreen mode

I shipped the float version. It worked. But the integer version is just better and I switched.

Within a tier, items are uniform. So once rollTier() returns 'covert', you just pick uniformly from the covert items in that case. Don't overthink it.

The 10% StatTrak roll

StatTrak is a separate roll after the item is picked, not a sixth tier:

function open(caseDef) {
  const tier = rollTier();
  const item = pickUniform(caseDef.items[tier]);
  const isStatTrak = Math.random() < 0.10;
  return { item, isStatTrak };
}

Enter fullscreen mode Exit fullscreen mode

I see a lot of clones get this wrong — they roll StatTrak as part of the tier weights and the math drifts. It's conditional, not categorical.

Float values: where most tutorials die

Each skin has a wear value from 0.0 (Factory New) to 1.0 (Battle-Scarred). The wear conditions:

Wear Float range
Factory New 0.00 – 0.07
Minimal Wear 0.07 – 0.15
Field-Tested 0.15 – 0.38
Well-Worn 0.38 – 0.45
Battle-Scarred 0.45 – 1.00

Here's the gotcha that wrecks most sims: float ranges aren't 0 to 1 for every skin. Each skin has a min_float and max_float clamped by the workshop creator. The AK-47 Vulcan has min=0.0, max=0.9. The AWP Asiimov has min=0.18, max=0.55 — meaning it cannot drop Factory New, ever. If your sim rolls AWP Asiimov FN, your sim is wrong.

Correct version:

function rollFloat(skin) {
  const raw = Math.random();
  return raw * (skin.maxFloat - skin.minFloat) + skin.minFloat;
}

function wearFromFloat(f) {
  if (f < 0.07) return 'Factory New';
  if (f < 0.15) return 'Minimal Wear';
  if (f < 0.38) return 'Field-Tested';
  if (f < 0.45) return 'Well-Worn';
  return 'Battle-Scarred';
}

Enter fullscreen mode Exit fullscreen mode

The skin-specific min/max is the entire reason float-capped skins (like a 0.00-something AK-47 Fire Serpent) trade for absurd money. They're rarer than the wear distribution alone suggests because the underlying float distribution is also clamped on the high end.

The spin animation bias trap

This is the one I want every developer building a loot reveal UI to internalize, because I've seen it shipped in production at sites that should know better.

You've seen the CS2 case opening animation: items scroll horizontally, decelerate, and stop on the result. The wrong way to build that:

// ❌ WRONG: roll position, derive result
function openCase() {
  const trackLength = items.length * itemWidth;
  const stopPosition = Math.random() * trackLength;
  animateTo(stopPosition);
  return getItemAt(stopPosition); // result depends on visual layout
}

Enter fullscreen mode Exit fullscreen mode

Why it's wrong: the items on the track aren't uniformly distributed by tier. Most slots are common items, very few slots are knives. If you roll a uniform position over the track, your effective drop rate is just whatever fraction of the visible track is occupied by each tier — not Valve's 79.92/15.98/3.20/0.64/0.26.

The right way:

// ✅ RIGHT: roll result, then animate to it
function openCase(caseDef) {
  const result = rollResult(caseDef);             // 1. determine outcome
  const targetIndex = placeResultOnTrack(result); // 2. position it
  const targetPx = targetIndex * itemWidth + jitter();
  animateTo(targetPx, { duration: 5000, easing: 'ease-out' });
  return result;                                  // 3. show what we already decided
}

Enter fullscreen mode Exit fullscreen mode

The track is just visual theater. The result is decided first. The animation lands on it.

This sounds obvious but if you grep enough open-source case opener clones on GitHub you'll find the wrong pattern in real code. It feels right because it looks random. It just isn't the right random.

Pattern indices and the Doppler problem

Each item also rolls a pattern index from 0 to 999. For most skins this affects the visual layout but not the value. For some skins — Case Hardened (blue gems), Crimson Web (full webs), Fade (100% fades), and especially Dopplers — pattern is everything.

Dopplers are nasty because the same skin name maps to different "phases" depending on pattern:

// Phase mapping for one Doppler variant (illustrative — exact ranges vary by knife)
const DOPPLER_PHASES = {
  ruby:        [418],
  sapphire:    [419],
  blackPearl:  [420],
  phase1:      [415],
  phase2:      [416],
  phase3:      [417],
  phase4:      [421],
};

function dopplerPhase(item, patternIndex) {
  for (const [phase, indices] of Object.entries(item.phases)) {
    if (indices.includes(patternIndex)) return phase;
  }
  return null;
}

Enter fullscreen mode Exit fullscreen mode

Phase distribution isn't uniform — Ruby/Sapphire/Black Pearl are ~5% combined, the rest split among phases 1-4. If you implement Dopplers as a single skin with a uniform pattern roll, your sim will show Ruby Karambits at like 0.1% rate and people will (correctly) call you out.

I model each Doppler variant as essentially seven distinct items at the right relative weights. It's annoying but it's the only way to get realistic pulls. You can see the full case list at case-sim.com/cases if you want to compare.

What scale actually breaks

123M rolls means a lot of things you wouldn't think about start to matter:

Database writes. I do not write a row per opening. That would be insane. Aggregate counters per case, per item, per day. The "global unbox feed" people see is a sliding window of recent rolls held in memory and trimmed aggressively. If you're building anything in this space, design your schema around aggregates from day one.

Rare-item flexing. When someone pulls a Dragon Lore (1 in 3,906 in a Souvenir Cobblestone package), they take a screenshot. They post it. The screenshot shows your URL. This is great for you — but it also means your knife/glove pull endpoint gets traffic spikes. Cache pulled-item OG images and pre-render the share cards.

Float floors. I had a bug where extremely low floats (below 0.001) would render as scientific notation in the UI: 1.234e-4 instead of 0.000123. Looked like garbage on share cards. Always format floats explicitly:

const formatFloat = (f) => f.toFixed(6).replace(/0+$/, '').replace(/\.$/, '');

Enter fullscreen mode Exit fullscreen mode

Trust. Once you publish drop rates, people will run their own statistical tests. Someone DM'd me a chi-square goodness-of-fit on 50,000 of their personal openings. It checked out (thank god). Have your random source be auditable — prefer crypto.getRandomValues() over Math.random() if you can, even though for sim purposes Math.random is mathematically fine.

function secureRandom() {
  const arr = new Uint32Array(1);
  crypto.getRandomValues(arr);
  return arr[0] / 0xFFFFFFFF;
}

Enter fullscreen mode Exit fullscreen mode

A few things I'd do differently

If I rebuilt today:

  1. Integer weights from day one. Every probability lives as parts-per-10000 or parts-per-1000000. Floating point in probability code is just asking for it.
  2. Decouple the item pool from the case definition. I had cases hard-referencing item objects. When Valve added new wear ranges or float caps, I had to touch every case. Now the case is just a list of item IDs + tier weights, and the item registry is the source of truth.
  3. Test the invariant. Every case definition has a unit test that asserts tier weights sum to exactly 10000. Cheap to write, catches stupid copy-paste mistakes when adding new cases.
test.each(allCaseDefs)('case %s tier weights sum to 10000', (caseDef) => {
  const total = Object.values(caseDef.tiers).reduce((a, b) => a + b, 0);
  expect(total).toBe(10000);
});

Enter fullscreen mode Exit fullscreen mode

That test has caught me twice.

Wrap

If you're modeling weighted-random anything — loot, gacha, traffic splitting, even A/B variants — the lessons from running 123M case openings of Counter-Strike 2's official cases are basically:

  • Use integer weights, not floats. Probability sums need to be exact, not "close enough."
  • Roll the result first, then animate to it. Visual position should never derive the outcome.
  • Per-skin float ranges matter more than tier weights for determining real-world rarity.
  • Conditional rolls (StatTrak, Doppler phase) are not extra tiers. Don't shoehorn them in.
  • Test that your weights sum to your invariant. Every time. In CI.

If you want to see how this all pans out in practice — opening real cases with real Valve odds, no money, no signup — that's literally what I built case-sim for. Otherwise, take the code patterns and go build your own thing. The world has room for more weighted RNG done right.

Hit me up in the comments if you've shipped something similar and ran into different gotchas. Always curious what other people hit.