Originally Posted by KliPPy
As you can see, the chances in the array add up to 2.0 (or 200% percent), but in that case if you divide them by 2 they will add up to 1.0.
It's pretty much what Fysiks did but with the mapping step so input values don't have to add up to 100%.
Well, I assumed that adding up to 100 was a trivial task which is should be.
If you don't want to do the math beforehand, you can simply replace
i = sum = 0
for( i = 0; i < count; i++ )
sum += itemChances[i]
rand = random(sum)
But realize that if they don't add up to 100, the values basically have no meaning, they're just arbitrary. Though, I can think of some minor use cases to do this to reduce the amount of math required to adjust probabilities.
Originally Posted by Airkish
It always return first number from array
Did you even try mine? It's super simple and it works. You can play around with the numbers to get what you need. If you don't know how to do the adding up to 100 thing, you can do the code replacement I mentioned above. It will be more efficient than Klippy's function.