1) Assign weight and order to each item.
> 5 Sword
> 10 Shield
> 4 Potion
> 1 Wand
2) Sum together their weights (X=20)
3) Prepare table by summig giving each item index that is sum of all previous weights
> 0 Sword
> 5 Shield
> 15 Potion
> 19 Wand
You will have all this prepared beforehead in data structure.
3) Roll X sided dice and find first item in table that has lower index than that.
If you roll 1, 2, 3, 4, 5 you get sword.
If you roll 16, 17, 18, 19 you get potion and 20 gets you wand.
You can very easily find right index by bisecting the table with log(N) performance.
You can very easily fine tune chance by using bigger numbers.
> 4154 Nothing
> 15556 Trash item
> 550 Sword
> 984 Shield
> 156 Potion
> 1 Wand
Gives you
> 19.4% Nothing
> 72.6% Trash item
> 2.5% Sword
> 4.5% Shield
> 0.7% Potion
> 0.004% Wand
Table would look like:
> 0 Nothing
> 4154 Trash item
> 19710 Sword
> 20260 Shield
> 21224 Potion
> 21400 Wand
You roll 21401 sided dice
Latest Answers