Problem:
I have items with weights. The higher the weight, the higher the likelihood that they will have an item. I need to have a simple and easy way to do this, based on the Java kernel (no third-party libraries, cans, etc.).
I did this for 2 elements, summing the scales, then randomly select a number using Math.random () in this range. Very simple. But for elements that are more than 2, I can either make more selections in the same skipped missing ranges, or I can recalculate the sum of the weights of the remaining items and select again (recursive approach). I think there may be something that can make it faster / cleaner. This code will be used again and again, so I'm looking for an effective solution.
In essence, this is like a randomized weight swap.
Some examples:
Ahas a weight of 1, Bhas a weight of 99. If I ran a simulation with this, I would expect to get BA99% of the time and AB1% of the time.
Ahas a weight of 10, Bhas a weight of 10, and Chas a weight of 80. If I were to run simulations with this, I would expect that there Cwill be the first item in the order 80% of the time, in these cases Athey Bwill have an equal chance of being the next character.
Any help or pointers in the right direction would be appreciated.
Additional information:
. 20 50 , , 1000. , , , , .