Can someone explain to me how this rarity system works?

I don’t at all understand how this works so I hope someone can clear it up for me:

2 Likes

This system uses a table to hold rarities where the probability of getting each item (key) in the table corresponds to its value in the table. The total of the values in the table is 100.

The loop is tracking a total, which will start at 0 and eventually reach 100 by adding values from the table. Each time a value is added to the total, the probability that the new total is now greater than or equal to randomNumber corresponds to the size of the value it has added, so keys with a greater value in the table have a greater chance of triggering the condition if randomNum <= counter, and selecting that key as the random rarity.

2 Likes

almost everyone use it, it simply adds to poll and then chooses random reward from table, if it’s bigger than poll then poll is your reward or smth

1 Like

Keys with greater value in the table will indeed have a greater chance of triggering the condition but aren’t these keys in the table percentages? If so then doing it this way, it’s not accurate

1 Like

The probability of any given key in the table being selected is equal to value / total of values in the table

1 Like
  1. Generates a random number
  2. Loops through the modules.rarities dictionary and adds counter with weight which I believe is the number inside of the value of the keys (the “keys” are Legendary, Epic, Rare, Common).
  3. Then it checks if the random number your generated is smaller or equal to the value of the key.

Weird thing is, shouldnt i return nothing if the number is greater than 50? Maybe Im misunderstanding something here

Edit: nvm i see counter isnt always zero i think

1 Like

it uses the cumulative value, which reaches 100, so it still works if its over 50

2 Likes

You could visualise it as these keys having “bounds”, and the key thats selected is the one where the random number lies within its bounds. In this example:

Legendary: 1 - 1
Epic: 2 - 20
Rare: 21 - 50
Common: 51 - 100

This makes it alot clearer to see that if we selected a random number between 1 and 100, the probability of the a given key being selected corresponds to the range of its bounds, and the range of its bounds equals its value in the original table

1 Like