<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/two-ways-to-roll-weighted-loot-obij8ob61" -->

---
title: Two Ways to Roll Weighted Loot | daily.dev
description: A walkthrough comparing two algorithms for implementing weighted loot tables in games: prefix sum with binary search, and the alias method. Starting from a...
canonical: https://daily.dev/posts/two-ways-to-roll-weighted-loot-obij8ob61
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Two Ways to Roll Weighted Loot | daily.dev
og:description: A walkthrough comparing two algorithms for implementing weighted loot tables in games: prefix sum with binary search, and the alias method. Starting from a...
og:url: https://daily.dev/posts/two-ways-to-roll-weighted-loot-obij8ob61
og:image: https://api.daily.dev/og/posts/OBIJ8OB61.png
og:image:alt: Two Ways to Roll Weighted Loot
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Two Ways to Roll Weighted Loot

**[git-amend](https://daily.dev/sources/git-amend)** · 12 min read · 0 upvotes · 0 comments

## Summary

A walkthrough comparing two algorithms for implementing weighted loot tables in games: prefix sum with binary search, and the alias method. Starting from a naive if/else-if implementation that doesn't scale, the explanation covers building cumulative weight arrays and binary searching them for the prefix sum approach, versus pre-partitioning items into probability/alias boxes for O(1) picks with the alias method. Includes a Unity demo comparing performance on a small five-item loot table, showing binary search actually outperforms alias method at small scale due to alias needing two random calls versus one plus cheap binary search steps. Also covers generating weights from an animation curve as a variant data source.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.youtube.com/watch?v=uZEFhS1VAlE>

## Questions this post answers

### What is the alias method for weighted random selection and how does it work?

The alias method pre-partitions weighted items into boxes so each draw resolves in O(1) time. Each box holds a probability of picking its own item plus an alias pointing to a backup item that fills the remaining space; picking requires choosing a random box, then a coin flip to decide between the box's own item and its alias, avoiding any scanning or binary search at draw time.

_Comparing algorithm trade-offs like this is easier with daily.dev surfacing game development deep dives._

### Is binary search or the alias method faster for a small weighted loot table with only five items?

Binary search over a prefix sum array is actually faster than the alias method on small tables like a five-entry loot rarity system. The alias method needs two random number calls per pick versus one random call plus roughly two binary search steps for prefix sum, and on tiny datasets that extra random call costs more than the search itself, though alias method still resolves in constant time regardless of table size.

_Developers choosing between algorithm trade-offs for small datasets can track deep dives like this via daily.dev._

### Why does a naive if-else chain for implementing loot tables become hard to maintain?

A naive if, else-if, else chain for loot rarity hardcodes thresholds that are order-dependent, so adding one more entry means recalculating several numbers and hoping nothing was missed. This makes it hard for designers to tune drop rates without touching code and hard to audit during game balancing, which is why separating data authoring from the sampling algorithm scales better.

_daily.dev helps developers refining game balancing logic keep up with better architectural patterns._

## Similar posts on daily.dev

- [Equalize All Prefix Sums](https://daily.dev/posts/equalize-all-prefix-sums-1vho3hqp8) · C\# Corner · 0 upvotes · 0 comments
- [The Dictionary Problem: How Do You Efficiently Look Up Things?](https://daily.dev/posts/the-dictionary-problem-how-do-you-efficiently-look-up-things--txhixcvpo) · The Polymathic Engineer · 1 upvotes · 0 comments

---

Tags: [#game-development](https://daily.dev/tags/game-development), [#algorithms](https://daily.dev/tags/algorithms), [#unity](https://daily.dev/tags/unity), [#data-structures](https://daily.dev/tags/data-structures), [#binary-search](https://daily.dev/tags/binary-search)

[View this post on daily.dev](https://daily.dev/posts/two-ways-to-roll-weighted-loot-obij8ob61)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Two Ways to Roll Weighted Loot","url":"https://daily.dev/posts/two-ways-to-roll-weighted-loot-obij8ob61","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/two-ways-to-roll-weighted-loot-obij8ob61"},"datePublished":"2026-08-30T13:06:11.367Z","dateModified":"2026-08-30T13:06:33.661Z","description":"A walkthrough comparing two algorithms for implementing weighted loot tables in games: prefix sum with binary search, and the alias method. Starting from a...","image":"https://i.ytimg.com/vi/uZEFhS1VAlE/sddefault.jpg","thumbnailUrl":"https://i.ytimg.com/vi/uZEFhS1VAlE/sddefault.jpg","isAccessibleForFree":true,"articleSection":"git-amend","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"git-amend","logo":"https://media.daily.dev/image/upload/s--dR-FewbM--/f_auto,q_auto/v1784447928/logos/git-amend?_a=BAMAMicg0","url":"https://daily.dev/sources/git-amend"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/two-ways-to-roll-weighted-loot-obij8ob61","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"game-development,algorithms,unity,data-structures,binary-search","timeRequired":"PT12M","video":{"@type":"VideoObject","name":"Two Ways to Roll Weighted Loot","description":"A walkthrough comparing two algorithms for implementing weighted loot tables in games: prefix sum with binary search, and the alias method. Starting from a...","thumbnailUrl":"https://i.ytimg.com/vi/uZEFhS1VAlE/sddefault.jpg","uploadDate":"2026-08-30T13:06:11.367Z","duration":"PT12M","url":"https://api.daily.dev/r/OBIJ8OB61","embedUrl":"https://www.youtube.com/embed/uZEFhS1VAlE"}}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"git-amend","item":"https://daily.dev/sources/git-amend"},{"@type":"ListItem","position":3,"name":"Two Ways to Roll Weighted Loot"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/two-ways-to-roll-weighted-loot-obij8ob61#faq","mainEntity":[{"@type":"Question","name":"What is the alias method for weighted random selection and how does it work?","acceptedAnswer":{"@type":"Answer","text":"The alias method pre-partitions weighted items into boxes so each draw resolves in O(1) time. Each box holds a probability of picking its own item plus an alias pointing to a backup item that fills the remaining space; picking requires choosing a random box, then a coin flip to decide between the box's own item and its alias, avoiding any scanning or binary search at draw time. Comparing algorithm trade-offs like this is easier with daily.dev surfacing game development deep dives."}},{"@type":"Question","name":"Is binary search or the alias method faster for a small weighted loot table with only five items?","acceptedAnswer":{"@type":"Answer","text":"Binary search over a prefix sum array is actually faster than the alias method on small tables like a five-entry loot rarity system. The alias method needs two random number calls per pick versus one random call plus roughly two binary search steps for prefix sum, and on tiny datasets that extra random call costs more than the search itself, though alias method still resolves in constant time regardless of table size. Developers choosing between algorithm trade-offs for small datasets can track deep dives like this via daily.dev."}},{"@type":"Question","name":"Why does a naive if-else chain for implementing loot tables become hard to maintain?","acceptedAnswer":{"@type":"Answer","text":"A naive if, else-if, else chain for loot rarity hardcodes thresholds that are order-dependent, so adding one more entry means recalculating several numbers and hoping nothing was missed. This makes it hard for designers to tune drop rates without touching code and hard to audit during game balancing, which is why separating data authoring from the sampling algorithm scales better. daily.dev helps developers refining game balancing logic keep up with better architectural patterns."}}]}
```

