- Joined
- Aug 18, 2009
- Messages
- 4,097
In ORPGs for example, enemies may drop random loot and furthermore that loot may be exclusive as to exactly spawn a single item. For this purpose, they possess a table which governs the types of all possible items and their chances/weight to appear.
Now how do you roll for an item? One option would be to accumulate the weights first, determine a random int from that, then iteratively accumulate the weights again and stop if the sum becomes greater than what was rolled.
Another possibility is to directly assign the same item to multiple slots in a data structure depending on its weight. In the above scenario, you could scale the table down for optimization. For example
{itemA: 3; itemB: 6; itemC: 9} is as good as {itemA: 1; itemB: 2; itemC: 3}
in terms of chances. The relative weight for each element remains the same. However, this of course means you may have to store a lot in the container structure and it requires a heavier init.
As a third simple option I want to state you could also roll for each item individually with their respective chance, multiply the result with the weight of the item and check for the highest product.
Anyway, that's iterative too and needs more randomizations.
My question is if there is a better, efficient approach or maybe a hybrid form that neither inflates the memory too much nor has O(n).
Additionally, in contrast to the described randomization scenario, which is somewhat different, I depict a 2nd task:
You know the unit limit events, unit's mana becomes greater than 100 for example. If you were to reconstruct this event yourself, you would hook onto the unit's mana points, you acquire the change from a source value to a target value.
There may be a lot of limit events on the same unit and with different bounds: unit's mana becomes greater than 20, greater than 50, greater than 100, greater than 499 etc. Iteratively checking each event for its limit and whether the current unit's state matches it, once more is not very convenient. One would rather like to determine the corresponding events directly by the value the unit's mana has adopted. And that's why having a method to assign to a number range, this case is even special because it greater than x would mean from x to infinity, would be a bless.
Thanks for reading/replying
Now how do you roll for an item? One option would be to accumulate the weights first, determine a random int from that, then iteratively accumulate the weights again and stop if the sum becomes greater than what was rolled.
Another possibility is to directly assign the same item to multiple slots in a data structure depending on its weight. In the above scenario, you could scale the table down for optimization. For example
{itemA: 3; itemB: 6; itemC: 9} is as good as {itemA: 1; itemB: 2; itemC: 3}
in terms of chances. The relative weight for each element remains the same. However, this of course means you may have to store a lot in the container structure and it requires a heavier init.
As a third simple option I want to state you could also roll for each item individually with their respective chance, multiply the result with the weight of the item and check for the highest product.
Anyway, that's iterative too and needs more randomizations.
My question is if there is a better, efficient approach or maybe a hybrid form that neither inflates the memory too much nor has O(n).
Additionally, in contrast to the described randomization scenario, which is somewhat different, I depict a 2nd task:
You know the unit limit events, unit's mana becomes greater than 100 for example. If you were to reconstruct this event yourself, you would hook onto the unit's mana points, you acquire the change from a source value to a target value.
There may be a lot of limit events on the same unit and with different bounds: unit's mana becomes greater than 20, greater than 50, greater than 100, greater than 499 etc. Iteratively checking each event for its limit and whether the current unit's state matches it, once more is not very convenient. One would rather like to determine the corresponding events directly by the value the unit's mana has adopted. And that's why having a method to assign to a number range, this case is even special because it greater than x would mean from x to infinity, would be a bless.
Thanks for reading/replying