0

So im looking for preferably the most efficient method for generating items from a list/array based on their weight multiple times and each time an item is chosen it's removed from the pool. I also want this method to have a 'luck' parameter (like a multiplier int/double) which changes the items' weight based on attributes they have. So i have like a list:

  • treasure1 w:1 m:1

    treasure2 w:5 m:2

    common item1 w:80 m:0

    common item2 w:50 m:0.5

    trash1 w:70 m:0

    trash2 w:150 m:-10

so i have the item and then 'w' weight for it and 'm' modifier for the luck parameter. Basically if i pass 'luck 3' it gets the value of luck and for each item it multiplies it with it's 'm' stat and adds it to the weight. So with luck 3 treasure1 will get weight 1 + luck*m which is 1 + 3 = 4 while also the trash2 item is going to get its weight reduced from 150 to 150 + -10x3 = 120. So the higher the luck the higher the chance for treasures and lower for trash.

What i have now: Right now i have a list with the items in it but for every single call i need to loop through the list, calculate their new weight based on the luck i passed and calculate the total weight of the list. Then i generate a random number 0 to totalWeight and start looping through the list again and subtract the current items weight from the generated value. If the value reaches 0 or lower i put that item into the result list and remove from the base list. After that i loop through the list and do the same (random number and keep subtracting) until i generated as many items as i wanted.

So is there a better method for this?

0 Answers0