r/howdidtheycodeit • u/SnappierSoap318 • Jan 04 '22
How do loot tables work?
I'm familiar with the fact that there is a list of items which have certain probabilities attached to them which all add upto 1, but how does a randomizer pick items from that list to add that item to a loot box?
55
Upvotes
45
u/_destron Jan 04 '22
Here is a pretty fantastic article on how to design a system to do things like this. As the article points out, for very small games you might be able to get away with a random number generator grabbing items out of list, but once you need to do things like individual probability and item sets then you'll need to get a bit more complex with it. Part 2 is great as well.
20
u/Emergency_Bedroom_96 Jan 04 '22
There is no need to have the probabilities sum up to 1. They just have to be relative to each other, ex. 100 and 1000 is 10x chance. You can then sum up all the probabilities and math.rand() it. If you think at a long line where segment of it are your items probabilities, the rand() you got will place inside one of the segments, that is your item.
5
u/thor_sten Jan 04 '22
Old-School TTRPG-Like: Make a list that features the more common items more often:
1-3:Dagger, 4-5:generic Sword, 6:Claymore, and then generate a random number 1-6, to pick from it.
Or (as this can get quite tedious to extend, let's say you want to give a tiny chance to get Excalibur...) make a list of weights that "creates" the list above at runtime.
Dagger: Weight 3
Sword: Weight 2
Claymore: Weight 1
Excalibur: Weight 0.1
Step 0: Create List from weights (just add up the weights above):
3 Dagger
5 Sword
6 Claymore
6.1 Excalibur
Step 1: Sum up all weights (3+2+1.0.1=6.1)
Step 2: Generate a random number <= sumOfweights say "5.9"
Step 3: Search the list until you find something that is below the random number in Step 2 Daggers =3. below 5.9 ? Nah Swords =5. below 5.9 ? Nah Claymore =6 below 6 ? Yep
5
u/LivelyLizzard Jan 04 '22
You can generate equally distributed numbers (e.g. from 1 to 1000) and associate a certain range with a certain item.
Example: The legendary sword has a drop chance of 0.1%, so you would need to draw a 1000 to get it. The common helmet has a drop chance of 10%, so you can draw anything from 1 to 100 to get the helmet. If you have another common item with 10% chance, you might assign it the range from 101 to 200.
4
u/Alawliet Jan 04 '22
It's not too complicated. If your probabilities add up to 1, most of the work is done. Generate a random number between 0 & 1.
Iterate through your list of items, and keep adding their probabilities. When the sum exceeds the random number generated, spawn the item whose probability u just added.
Ex. Loot table
Axe 0.25 Gun 0.3 Sword 0.3 Nuke 0.15
Your random number generator gives you 0.84
You add axe+gun+sword, 0.25+0.3+0.3 = 0.85 Since that exceeded your number you spawn the sword.
I like thinking of it as a bunch of blocks with width equal to their probability laid next to each other.
2
u/indigosun Jan 04 '22 edited Jan 04 '22
function pickWeighted (weights[])
Here's my pseudocode golf
Edit: don't write code on your phone
1
u/[deleted] Jan 04 '22
Loot table where each item has the same drop chance:
Loot table where the dagger is twice as likely to drop:
That's the basic principle.