Skip to content

Weighted Probability

Ryandw11 edited this page Mar 20, 2023 · 1 revision

The vast majority of CustomStructures' probability system is based on a Weighted Probability structure.

The way it works is that each item is assigned a probability weight. Let's take LootTables as an example:

LootTables:
  CHEST:
    lootTable: 5
    lootTableTwo: 4
    lootTableThree: 1

A weight is any positive integer value.

The way the probability works is that the weight of all possible items are added up to form the TotalWeight value. In the example above that would be: TotalWeight = 5 + 4 + 1 = 10. The probability of each item being selected is Weight/TotalWeight.

So the probability of lootTable being selected is 5/10 = 1/2 = 50%. The probability of lootTableTwo being selected is 4/10 = 40%. The probability of lootTableThree being selected is 1/10 = 10%.

Any time you add a new item, the probability of all the existing items shift. So if we add a lootTableFour with a weight of 2, the probability of the loot tables become:

  • P(lootTable) = 5/12 = 41.66%
  • P(lootTableTwo) = 4/12 = 33.33%
  • P(lootTableThree) = 1/12 = 8.33%
  • P(lootTableFour) = 2/12 = 16.66%

The benefit of this system is that, unlike the structure probability system, an item is always selected. So in the case of LootTables, a LootTable is always guaranteed to be selected, no matter what the probabilities are.

Systems That Use Weighted Probability

  • Loot Tables
  • Sub Schematics

Internal Implementation

The internal implementation of this system is represented by the RandomCollection<E> class.

Clone this wiki locally