Im trying to create a weighted random system for my game in unity using C#.
Player starts with N events, but as the game goes on the amount of events increases. At first all the events are equally likely, probability 1/N, but the player can use modifier to make some event more likely, like some events have double the probability to occur or in some cases they can force an event to happen no matter what.
I have found that widespread approach to do weighted randoms is to sum all the weights, generate random number up to the sum of weights and then iterate over choises and accumulate the weights until the accumulator > generated value.
Now i might be overthinking this, but to me it seems like there are some issues with this approach.
For example: Player has 7 choises, each has a weight of 1/7 and sum weights is 1. Now if 1 event gets a double modifier its new probability is 2/7 making the sum of weights 8/7. This leads me to think that i should subtract 1/42 from the weights of the other events to keep the sum as 1. Otherwise its not actually double probability, i think.
Another example: Player forces an event to occur 100%. This means 1 event has weight of 1 and all the other has weight of 0. Now it depends a bit of the implementation of the Random function but in theory nothing stops the random from spewing out 0 and then depending on the order of the events any event with 0 can happen.
If you have notices i have kept the weights between 0 and 1. This allows me to use weights and probabilities interchangeably since weight of 1/7 also means probability of 1/7. Also this way i dont have to calculate the sum of the weights, since its always 1.
Am i overthinking the problem and its actually not that complex?
Additional note: Some of you might suggest ordering the list of events based on the weights from largest to smallest. Since the RNG system is core of the gameplay and happens very often it might not be wize in performance perspective to sort the events, given that there might be different modifiers in play everytime a new random is pulled.
Also if in the 7 choise example, when i double chance of 1 event and instead of 2/7 i would use 2/8 and everything else is 1/8 then its actually not double the probability as 2/7 > 2/8.